Breaking

Tuesday, April 16, 2019

April 16, 2019

What is the data type of super key word?

What is the data type of super key word?

Super keyword is employed for differentiating the bottom category options with derived category options. Super keyword is inserting a vital role in 3 places. they're at variable level, at technique level and at creator level.

Super at Variable level

Whenever we have a tendency to inherit the bottom category members into derived category, there's a prospect that base category members area unit like derived category members.

In order to tell apart the bottom category members with derived category members within the derived category, the bottom category members are going to be preceded by a keyword super.
What is the data type of super key word
LEARNWITHARYAN

Syntax for super at Variable level:

super. base category member name
For example:

class Bc
;
class Dc extends B.C.
{
    int a;
    void set (int x, int y)
    {
        super.a=x;
        a=y; //by default 'a' is preceded with 'this.' since 'this.' represents current category
    }
    void add ()
    
};
class InDemo

};

Super at technique level

Whenever we have a tendency to inherit the bottom category strategies into the derived category, there's a prospect that base category strategies area unit like derived strategies.

To differentiate the bottom category strategies with derived category strategies within the derived category, the bottom category strategies should be preceded by a keyword super.

Syntax for super at technique level:

super. base category technique name
For example:

class Bc
show ()
    {
        System.out.println ("BASE category - show...");
    }
};
class Dc extends B.C.
show ()
    {
        super.display (); //refers to base category show technique
        System.out.println ("DERIVED category - show...");
    }
};
class InDemo

};

Super at creator level

Whenever we have a tendency to develop any inheritance application, we have a tendency to use to make continually object of bottom most derived category. after we produce associate degree object of bottom most derived category, it in turns calls its immediate super category default creator and it in turns calls its prime most super category default creator. Therefore, in JAVA surroundings, constructors are going to be known as continually from bottom to prime and also the execution starts from prime to bottom.

Consider the subsequent multi level inheritance:


Super () is employed for vocation super category default creator from default creator or from parameterized creator of derived category. it's ex gratia.

Super (...) is employed for vocation super category parameterized creator either from default creator or from parameterized creator of derived category. it's continually obligatory.

Rules:

Whenever we have a tendency to use either super () or super (...) in derived category constructors they have to be used as initial statement.


1. Whenever we wish to decision default creator of base category from default creator of derived category mistreatment super () in default creator of derived category is ex gratia.

For example:

class Bc
{
    Bc ()
    {
        System.out.println ("I AM FROM BASE category...");
    }
};
class Ibc extends B.C.
{
    Ibc ()
    {
        System.out.println ("I AM FROM INTERMEDIATE BASE category...");
    }
};
class Dc extends Ibc
{
    Dc ()
    {
        super (); //optional
        System.out.println ("I AM FROM DERIVED category...");
    }
};
class InDemo3

};
2. Whenever we wish to decision the super category parameterized category from parameterized category of the derived category mistreatment super (...) in parameterized category of derived category is obligatory.

For example:

class C1
{
    int a;
    C1 (int a)
    {
        System.out.println ("PARAMETERIZED creator - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};
class C2 extends C1
{
    int b;
    C2 (int a, int b)
    {
        super (a);
        System.out.println ("PARAMETERIZED creator - C2");
        this.b=b;
        System.out.println ("VALUE OF b = "+b);
    }
};
class InDemo4

};
3. Whenever we wish to decision default creator of base category from parameterized category of derived category mistreatment super () in parameterized category of derived category is ex gratia.

For example:

class C1
{
    int a;
    C1 ()
    {
        System.out.println ("PARAMETERIZED creator - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};
class C2 extends C1
{
    int b;
    C2 (int b)
    {
        super (); //optional
        System.out.println ("PARAMETERIZED creator - C2");
        this.b=b;
        System.out.println ("VALUE OF b = "+b);
    }
};
class InDemo5

};
4. Whenever we wish to decision parameterized category of base category from default creator of derived category mistreatment super (...) in default creator of derived category is obligatory.

For example:

class C1
{
    int a;
    C1 (int a)
    {
        System.out.println ("PARAMETERIZED creator - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};
class C2 extends C1
{
    int b;
    C2 ()
    {
        super (10);
        System.out.println ("DEFAULT creator - C2");
        this.b=20;
        System.out.println ("VALUE OF b = "+b);
    }
};
class InDemo6

};
Best example for the higher than given rules:

class Bc
{
    Bc ()
    {
        System.out.println ("BASE category - DEFAULT CONSTRUCTOR");
    }
    Bc (int x)
    {
        this ();
        System.out.println ("BASE category - PARAMETERIZED CONSTRUCTOR");
    }
};
class Ibc extends B.C.
{
    Ibc ()
    {
        super (100);
        System.out.println ("INTERMEDIATE BASE category - DEFAULT CONSTRUCTOR");
    }
    Ibc (int x)
    {
        this ();
        System.out.println ("INTERMEDIATE BASE category - PARAMETERIZED CONSTRUCTOR");
    }
};
class Dc extends Ibc
{
    Dc ()
    {
        this (10);
        System.out.println ("DERIVED category - DEFAULT CONSTRUCTOR");
    }
    Dc (int x)
    {
        super (10);
        System.out.println ("DERIVED category - PARAMETERIZED CONSTRUCTOR");
    }
};
class StDemo

};

Sunday, April 14, 2019

April 14, 2019

what are classes and how we can declare classes in Java

 what are classes and how we can declare classes in Java

Hello guys welcome to the next my Blogger on Java programming for beginners till now we have seen the procedural part of Java now we will see some of the object-oriented aspects of Java and first of all we will see what are classes and how we can declare classes in Java.
We can use categories in Java thus 1st of all
What is a class
It contains variables and methods. Usually (but not always) category used as templet for making objects. Class is structure unit of Java program. In other words, Java application consists of classes.
We may have noticed that whenever we have created our first program it was a class and inside this class we were calling this main method so every Java project has a minimum of one category and one main technique and sophistication is largely an information structure or kind in aspect that you'll be able to outline some variables that ar known as member variables and methods which are called member methods and the most important property of classes are you can create instance or object from class so you can maintain different states of at last using these instances or objects so let us see how we can define a class which will be a new class and how we can use it with our main category that is my category thus as an example we would like create|to form|to create} thusme student arrangement and for that we are going to make a category so simply go to your project if you are using eclipse and go to that package inside which you will be having a class my class in my case just go to the package right click a similar package and visit new and make a brand new category and this category name we are going to name it as student okay and leave everything as default and click finish and now we have package so we have two classes now my class is the first class which we were working on and the second class is called student dot Java and this is often be} the second category we've got created currently currently this category will have its member variables and strategies thus as an example what can be the traits of the student student can have his ID so ID is integer values I declare a variable called ID student can have name so I declare a variable called name and student can have age his age so I have declare student age okay so this is basically you have created a class and it can have member variables it can also have member methods that we will see in a moment but let's consider this is a basic student class which contains three variables ID name and age okay now to call this class in your main method in my my class you can just create an instance of or object of the class so how you can bang you'll be able to simply decision a similar name student category name simply copy this name here {and then|then|so|and thus} provide the name to your instance so as we have a tendency to have we give any name to our variables for example age name or something here also you can give the name to the students variable for example the name is mark we have a tendency to ar creating the info structure for mark is up to new student okay associated this primarily we've got created an instance of student category that we have a tendency to named as mark so this is a mark instance so mark here mark here is called object okay or some people also call it as instense okay instance so mark is here called object or instance and this object we can use to you know assign values to the member variables of the student class for example marks ID is so whenever you call this instance called mark and whenever you use this dot separator it will give you the options which is provided by eclipse to you so the student class contain age ID and name thus we are going to select ID student ID is up to as an example a 1 mark is our 1st student and within the same method you can call mark dot name should be mark dot name is equal to name is mark we already know and the age so mark dot age is equal to and he is fifteen year old for example so what we have done is we have created an instance of student class and we name it as mark and then to this instance we have created or we have assigned some values and these values we can you know access these values with the same name small dot ID mark dot name and math dot age so you can just write this Oh control shift and then you can just write mark dot name is so you use plus or concatenation operator is {and then|then|so|and thus} and so mark dot age so we are going to print mark is no matter his age is years recent okay years recent okay and after we run this program it will print mark is 15 years old okay in a similar way we can create different instances of our classes and so what we can do is we can will be able to simply copy a similar mark student category {and we have a tendency to|and that we have a tendency to} will produce a brand new object known as this point we decision it Tom okay thus here tom is that the second instance of the scholar category okay and his ID is to call is Tommyhis ID is to name is Tommy itself and the age is for example 14 okay so in here we can once again make the copy of this print line method and this time we will just replace mark by tom and now you can see it prints tom is 14 year old and is there something wrong here yeah here we have not modified this instance name here once more we'll run the program and say is mark is fifteen year recent and tom is fourteen years recent okay so employing a category will|we will|we square measure able to} outline completely different object and that we can outline these object values of object range variable values that are freelance of every alternative thus this instance values which are ID Tom's ID Tom's name and Tom's age is independent of Mark's ID Mark's name and marks age okay so this is very important property of object that it can store its own state and it's independent of the other instance right now in you know in java programming or programming in general we never use or this kind of usage direct usage of variables are discouraged and why it's discouraged because any other person or whoever is whoever is using your code can modify these values so we use some you know some methods or some functions which can get or set these values okay so what I mean by getting and setting these values is I can go to this class called student and in here I can just use I can just go to the source tab and in here I can go to so we will go to the source and just go to generate getters and setters here and here we can see operation is not applicable in current plus okay and in here we will just use this and click once again getters and setters so just click one variable name or variable then only this will work so go to source once again and create getters and setters and in here select all so we are basically making getter and setter method for every value because we do not want to use or we do not want to access these variables directly we just create getters and setters for all of them and whenever we click OK you can see here Java has created us these Gators and setters for all these values ok and what this is basically doing is it has we whenever we want to you know assign some value to the ID we will use set ID method and whenever we want to get the value of ID we will use get method and this is called encapsulation of a variable and this is important because we do not want to give access to our variables to some other user which is unknown to us right and so now what we are going to do is we have generated these getters and setters in between our variables so we can just cut these values and paste it here and these Caterson settles we've got generated for all the worths right thus currently after we need to assign some value to the mark instance rather than this mark dot ID we will just use mark dot set ID was set ID and then we can give any ID to mark and this value assignment is we are not going to use in the same way we will use marks dot set name and this name we will give to mark and in the same way we will assign age to mark thus mark dot set age okay AJ is fifteen right and in a very same approach we'll do one thing with Tom's instance conjointly thus tom dot set ID tom dot set name so tom dot set name we will assign this name to Tom and Tom dot set age same thing here so basically what we have done we have used instead of direct assignment we are assigning values using these skaters and setters and this protects your data from corrupting if you somebody wants to corrupt or your daughter or something like this okay and to get these value you just call mark dot get name and mark dot get age okay and same we will do with our Tom instance and whenever we run the program result will be same but this is more preferred while you are accessing or assigning the values to the member variables thus this can be however categories works in Java.

Wednesday, April 10, 2019

April 10, 2019

Abstract methodology in Java Program

Abstract methodology in Java Program


Abstract categories are classes that contain one or a lot of abstract strategies. the associate abstract methodology may be a method that's declared however contains no implementation. Abstract categories might not be instantiated, and need subclasses to supply implementations for the abstract strategies. Let's examine the associate example of an abstract category, associated with an abstract methodology.

A category that contains the abstract keyword in its declaration is understood as an abstract class.

Abstract categories could or might not contain abstract strategies that are., strategies while not a body ( public void get(); )
But, if a category has a minimum of one abstract methodology, then the category should be declared abstract.
If a category is said abstract it can not be instantiated.
To use an associate abstract category you have got to inherit it from another class, give implementations to the abstract strategies in it.
If you inherit associate abstract category you have got to supply implementations to all or any the abstract strategies in it.
An abstract category may be a class that's lacking some implementation and thus cannot be instantiated on its own while not its full practicality being completed.

Example of an associate abstract category with an abstract (or un-implemented) method:

//example code is in Java
//class should be marked as abstract as a result of it contains the associate abstract methodology
public abstract category Animal-public String name;

    //returns String containing Animal's name
    public String name_toString()come back "Name is: " + name;
    }

    //returns the distinctive noise the Animal makes
    public abstract void noise(); //method wasn't enforced

}
Abstract methodology in Java Program
LEARNWITHARYAN

Abstract categories are wont to give an overview of a category. not like associate interface (which alone instantiates the methods) it will give some practicality, i.e. some components of the category are enforced and a few parts are simply printed with a way declaration. within the example, higher than, the name_toString methodology is already enforced as a result of this practicality can stay the identical for any category that extends off of Animal class.

To use associate abstract category all you wish to try to is complete its practicality by extending off of it:

public category Extended extends Animal{

     public abstract category Animal-public String name;

        public String name_toString()come back "Name is: " + name;
         }

         public abstract void noise(); //method wasn't enforced
     }
   
   public category Cat extends Animal

    }

    public static void main(String[] args)doesn't work as a result of category Animal is abstract
       Cat c = new Cat(); //this works as a result of Cat isn't abstract

    }
}




Suppose we have a tendency to were modeling the behavior of animals, by making a category hierarchy that started with a base class known as Animal. Animals are capable of doing various things like flying, creating by removal and walking, however, there are some common operations furthermore as feeding and sleeping. Some common operations are performed by all animals, however in a very totally different manner furthermore. once an associate operation is performed in a very totally different manner, it's an honest candidate for associate abstract methodology (forcing subclasses to supply a custom implementation). Let's examine a really primitive Animal base category, that defines an associate abstract methodology for creating a sound (such as a dog barking, a cow mooing, or a pig oinking).

public abstract Animal
one thing with food... 
   }

   public void sleep(int hours)
   a thousand milliseconds * sixty seconds * hour * hours
  Thread.sleep ( a thousand * sixty * 60 * hours);
 }
 catch (InterruptedException ie)  
   }

   public abstract void makeNoise();
}
Note that the abstract keyword is employed to denote each associate abstract methodology and an abstract category. Now, any animal that desires to be instantiated (like a dog or cow) should implement the makeNoise methodology - otherwise, it's not possible to make an associate instance of that category. Let's examine a Dog and Cow taxon that extends the Animal class.

public Dog extends Animal

}

public Cow extends Animal

}
Now you'll be speculative why not declare associate abstract category as an interface, and have the Dog and Cow implement the interface. certain you'll - however you'd conjointly must implement the eat and sleep strategies. By victimization abstract categories, you'll be able to inherit the implementation of different (non-abstract) strategies. You can't try this with interfaces - associate interface cannot give any methodology implementations
Example:
  1. abstract class GraphicObject {
  2. int x, y;
  3. ....
  4. void moveTo(int newX, int newY) {
  5. ... // implemented here
  6. }
  7. abstract void draw(); // to be implemented by subclass
  8. abstract void resize(); // to be implemented by subclass
  9. }
  10. class Circle extends GraphicObject {
  11. void draw() {
  12. ...
  13. }
  14. void resize() {
  15. ...
  16. }
  17. }
  18. class Rectangle extends GraphicObject {
  19. void draw() {
  20. ...
  21. }
  22. void resize() {
  23. ...
  24. }
  25. }