Java4teachus

Sunday, August 18, 2019

super() and super(..) constructor in java

Super at Constructor Level


➧ We develop the inheritance application, it is always recommended to create an object of bottom most derived class because inheritance features of intermediate class.

➧ When we create of object of bottom most class, first we get the memory space for data members from Top Most Base class,  second we get the memory space for data members for Intermediate Base class, Last we get the memory space for data members Bottom Most Derived class.

➧ In whatever the order the memory space  is created, in the same order "values must be initialized" with respect to class name i.e we must initialized data members for Top Most Base class, Data members for Intermediate class, data members for Bottom Most Derived class, Other wise we get the compile time errors occur.

➧  In inheritance base applications constructors are calling from bottom to top and executing from top to bottom. This rule is established communication between the base class and derived class constructors.

⏩Let us consider the following diagram which makes to understand communication between base class constructor & derived class constructor.
Super Keyword at Constructor level in java.jpeg
Super  Keyword at Constructor level are classified into 2 type. They are:
  1. super( )
  2. super(...)
➧ super( ) : It is used for calling super class default constructor from the derived class constructor. 
➧ super(...) : It is used for calling super class parameter constructor from the derived class constructor.

Important Rule


When we use the super() and super(..)as a part of derived class constructor then super class must be always as a part of  executable statement in the derived class constructor otherwise we get compile time errors.

Following diagram possibility of using super() & super(..)
super()-and-super(..)-constructor-in-java.jpeg
Rule 1 & Rule 3,  whenever the derived class constructor want to call default constructor of base class,in the context of derived class constructors we write super(). It is optional to use because class contain single form of default constructor.

Rule 2 & Rule 4, Whenever the derived class constructor want to call parameterized constructor of base class in the context of derived class constructor we must write as super(..). It is mandatory to use because base class constructor contains multiple forms of parameterized constructor.

Program:


class TopMostBaseClass

{
TopMostBaseClass()
{
super( )    // Control goes to java.lang.Object( )
System.out.println("TopMostBaseClass   to   Derivedclass");
}
class IntermediateClass
{
IntermediateClass()
{
super(); // control goes to TopMoseBaseClass
System.out.println("IntermediateClass   to   Derived class");
}
class BottomMostDerivedClass
{
BottomMostDerivedClass()
{
super();
System.out.println("BottomMostDerivedClass  to  Derived class");
}
}
class SuperRule
{
public static void main(String args[])
{
BottomMostDerivedClass   b=new BottomMostDerivedClass();
}
}

Program:1 Baseclass to Baseclass


class BaseClass
{
String a;
BaseClass()
{
System.out.println("Base class to derived class");
a="Java4";
System.out.println("BaseClass:"+a);
}
}
class DerivedClass extends BaseClass
{
String b;
DerivedClass()
{
super();
System.out.println("Derived class to Derived class");
b="Teachus";
System.out.println("DerivedClass:"+b);
}
}
class SuperClass
{
public static void main(String args[])
{
DerivedClass d=new DerivedClass();
}
}

Program: 2

class BaseCLass
{
int a;
BaseClass(int a)
{
System.out.println("Baseclass to Derivedclass");
this.a=a;
System.out.println("value of a="+a);
}
}
class DerivedClass extends BaseClass
{
int b;
DerivedClass(int a,int b)
{
super(a);
System.out.println("Derivedclass to Derivedclass");
this.b=b;
System.out.println("value of b="+b);
}
}
class SuperClass1
{
public static void main(String[] args) 
{
DerivedClass d=new DerivedClass(10,20);
}
}

Thursday, August 01, 2019

Super Keyword at Method Level in java

Super Keyword at Method Level


Whenever we inherit the Base class method into Derived class, there is a possibility that base class method are similar to derived class method and JVM gets ambiguity problem. 

In order to differentiate between the base class methods and derived class methods, in the context of derived class and base class methods must be preceded by super keyword.

Syntax:
Syntax-of-super-keyword-at-method-level.jpeg
If we do not write super keyword before the base class method name then base class method name can be hidden in the content of derived class. that method will be considered by JVM as derived class method called by itself infinitely and recursive rest and gets run time error called as "Stack Over Flow Error".

Program: 1


class BaseClass

{
void display()
{
System.out.println("Java4");
}
}
class DerivedClass extends BaseClass
{
void display()
{
super.display();
System.out.println("Teachus");
}
}
class SuperEx
{
public static void main(String[] args) 
{
DerivedClass d= new DerivedClass();
d.display();
}
}
Output


Super at Method Overridden Level


We know that method overriding = Method heading is same + Method Body is different.    

The process of  pre-defining the  original method of  base class into various derived class for performing different operations  is called "Method Overriding". To applying the method overriding concept, we  need to use the inheritance principle. 

Program: 2

class Circle
{
void draw()
{
System.out.println("Draw Circle--main method");
}
}
class Rectangle extends Circle
{
void draw()
{
super.draw();
System.out.println("Draw Rectangle--Overriden method");
}
}
class Square extends Rectangle
{
void draw()
{
super.draw();
System.out.println("Draw Square -- Overiding Method");
}
}
class Shapes
{
public static void main(String[] args) 
{
Square s=new Sqaure();
s.draw();
}
}
Output:
====================
Draw Circle -- main method.
Draw Rectangle -- overridden method
Draw Square --  Overridden method
====================

Tuesday, July 30, 2019

Super Keyword at Variable Level in Java

Super Keyword in java



Super is a keyword  or reference variable in java which is used  to refer the parent object.  Super is an implicit keyword which is created by JVM for each and every program during compile time to playing the 3 important role. They are:
  1. Super keyword at data members or variable level.
  2. Super keyword at Method level.
  3. Super keyword at Constructor Level.

Purpose Of Super Keyword


Whenever we inherit the base class features into derived class, There is a possibility that base class features are similar to derived class and JVM gets ambiguity problems.  In Order to differentiate between the base class features  and derived class features must be preceded by super keyword. 

Syntax
Syntax-of-Super-Keyword-in-java.jpeg


Super Keyword at Data Member/Variable Level.


Whenever the derived class inherit the base class data members, there is a possibility that base class data members are similar to the  derived class data members and JVM gets ambiguity.

In Order to differentiate between base class data members  and derived class data members, in the context of derived class and base class data members must be preceded by super keyword.

Syntax
Syntax-of-Super-keyword-at-Variable-level-in-java
If we do not write a super keyword before base class data members then that data members is consider by JVM like as derived class data members and base class data members will be hidden.

Program:1

class BaseClass
{
int a;
}
class DerivedClass extends BaseClass
{
int a,b;
void set(int x,int y)
{
super.a=x;
this.a=y;
}
void add()
{
b=super.a+this.a;
}
void display()
{
System.out.println("value of Baseclass :" + super.a);
System.out.println("value of Derivedclass :" + this.a);
System.out.println("Summ :"+b);
}
}
class BaseDerivedClassEx
{
public static void main(String[] args) 
{
DerivedClass d=new DerivedClass();
d.set(10,20);
d.add();
d.display();
}
}
Output
Super-At-Variable-level-program.jpeg

Program :2

class First
{
String name="Java4";
}
class SecondName extends First
{
String name="Teachus";
void display()
{
String merge=super.name+name;
System.out.println("Full Name: "+ merge);
}
}
class FullName
{
public static void main(String[] args) 
{
SecondName s=new SecondName();
s.display();
}

}
Output
Super-Keyword-at-Variable-level-in-java.jpeg


Monday, July 22, 2019

Types Of Relationships in java

Relationships in Java

The purpose of relationships in java is that how to reduce the features from one to another class. In java programming language we have 3 types of relationships. They are:
  1. Is-A relationship.
  2. Has-A Relationship.
  3. User-A Relationship.
➤ Is-A Relationship

In Is-A Relationship, one class is obtain the features of another class by using  inheritance concept with the help of extends keyword.

Example
Is-A-Relationship-in-java.jpeg

Is-A relationship always follows the logical memory management. The limitation of Is-A relationship is that unable to solve the Ambiguity problem which is available in multiple inheritance and it can be solved by "HAS-A" relationship.

program:


class Salary
{
float x= 20000.0f;
}
class Bonus extends Salary
{
 float y=2000.0f;
}
class EmployeSal
{
public static void main(String l[])
{
Bonus s=new Bonus();
System.out.println("Salary is:"+s.x);
System.out.println("Bonus is:"+s.y);
}
}

OutPut
Salary is : 20000.0
Bonus is : 2000.0

➤ HAS-A Relationship


In HAS-A Relationship an object of one class is created as a data member in the content of another class.

HAS-A-Relationship-in-java.jpeg
Here the relationship Between C1 & C2  is "HAS-A Relationship". It is always follows the physical memory management.

The advantages of HAS-A relationship is that to eliminate the ambiguity problems which is shown in the below example.

HAS-A-Relationship-Example-in-Java.jpeg
The above image has invalid code segment  and can be written as  the follows by applying "HAS-A" Relationship  as  valid code.
HAS-A-Relationship-example-in-java.jpeg
Program

class Employee
{
    float salary=20000.0f;
}
class Developer extends Employee
{
    float Bonus=2000.0f;
}
class  EmployeeDeveloper
{
    public static void main(String l[])
    {
        Employee e=new Employee();
        Developer d=new Developer();
        float a=e.salary+d.Bonus;
        System.out.println("Employee Salary :"+a);
    }
}


OutPut

HAS-A-Relationship-in-java.jpeg
➤ User-A Relationship

In User-A Relationship method of one class is using of another class the relationship between two classes.

USER-A-Relationship-in-java.jpeg
Program

class Employee

{
    float salary=20000.0f;
}
class  Salary extends Employee
{
    void show()
    {
        float bonus=2000.0f;
        float totalsal=salary+bonus;
        System.out.println("DevloperSalary :"+totalsal);
    }
}
class DevloperSalary
{
    public static void main(String l[])
    {
        Salary s=new Salary();
        s.show();
    }
}
Output
USER-A-Relationship-program-in-java.jpeg

Note 1: The default relationship in java "IS-A Relationship" because  for each every class in java their exist implicit predefined super class called   java.lang.Object.

 
Note 2: System.out  is a universal example for "HAS-A" Relationship. Here out is an object of PrintStream class and it is created as static data member class called System. The Relationship between the System PrintStream called  "HAS -A" Relationship.


Note 3: Each and every execution logic method (main()) of execution class is using logic of business logic class. Relationship between execution logic class and business logic class is called "USER-A".

Tuesday, July 16, 2019

Multiple inheritance and Hybrid Inheritance in Java

Multiple Inheritance



Definition

If a class is derived from "Two or more classes", it can be called as multiple inheritance. (OR) In this inheritance their exist multiple base classes and one single derived class. The below example shows the multiple inheritance. 

Example
Multiple-Inheritance-In-Java.jpeg

➤ In Java programming never support multiple inheritance to the concept of classes because it  contain ambiguity problems.
➤ Multiple inheritance does support in java by the concept of interfaces.

Hybrid Inheritance

Definition

hybrid inheritance is equal to the combination of any type of inheritance type. In this combination one of the combination is multilevel inheritance and multiple inheritance is called as hybrid inheritance which is shown below image.

Example:1
Hybrid-Inheritance-In-Java.jpeg
 Example:2
Hybrid-Inheritanc-In-Java.jpeg
➤ In Java programming never support hybrid inheritance to the concept of classes because it  contain ambiguity problems.
➤ Hybrid inheritance does support only in java by the concept of interfaces.

Friday, July 12, 2019

Hierarchical Inheritance In Java

Hierarchical Inheritance 


Definition

If two or more classes are derived from a class then it is called as hierarchical inheritance. (OR) In this inheritance their exist single base class and multiple derived class is called as Hierarchical Inheritance.

Hierarchical-Inheritance-In-Java.jpeg
Class Path
 
 Base class → Derived Class1
 Base class → Derived Class2
 Base class → Derived Class3

Note:- Single Inheritance, Multilevel Inheritance, Hierarchical inheritance are supported both classes and interfaces.

Syntax

class  A
{
-------
-------
}
class B  extends class A
{
-------
-------
}
class C  extends class A
{
-------
-------
}

➤  In the above example We have create one base class called A.
➤  Another derived class B calls the features from base class A by using extends keyword. which is shown below program.

Program

class Hero
{
    void show()
    {
        System.out.println("Hero");
    }
}
class Honda extends Hero
{
    void show1()
    {
        System.out.println("Honda");
    }
}
class Suziki extends Hero
{
    void show2()
    {
        System.out.println("Suziki");
    }
}
class HeroHonda
{
    public static void main(String[] args)
    {
        System.out.println("================");
        Honda h=new Honda();
        h.show();
        h.show1();
        Suziki s=new Suziki();
        s.show();
        s.show2();
        System.out.println("================");
    }
}

Output:-
Hierarchical-Inheritance-Programs-In-Java.jpeg

Program

class Lakshmi
{
    void show()
    {
        System.out.println("Lakshmi");
    }
}
class Lucky extends Lakshmi
{
    void show1()
    {
        System.out.println("Lucky");
    }
}
class Bujji extends Lakshmi
{
    void show2()
    {
        System.out.println("Bujji");
    }
}
class LakshmiLucky
{
    public static void main(String[] args)
    {
        System.out.println("================");
        Lucky l=new Lucky();
        l.show();
        l.show1();
        Bujji b=new Bujji();
        b.show();
        b.show2();
        System.out.println("================");
    }
}
Output:-
Hierarchical-Inheritance-Program-In-Java.jpeg