Java4teachus

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

Monday, July 08, 2019

Multilevel Inheritance In Java

Multi-Level Inheritance


Definition

If a class is derived from the class which is derived from the another class then it is also known as multi-level inheritance.  (or)  In this inheritance their exists single base class, single derived class and multiple intermediate base classes. which is shown in the below example.

Example
Mutlilevel-Inheritance-In-Java.jpeg
Inheritance Path : Class1→Class2→Class3→Class4
 
Note:- In the above example Base class is one, Derived class is also one but Intermediate class are  multiple.

Syntax:

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

➧  In the above syntax  we have one Base class called class A.
➧ An Another class B called as Intermediate Base class and we call the all features of class A by using extends keyword.
➧  A  class C is an derived class calling from the features called Class B by using extends keyword.

Program

class Alpha
{
    void show()
    {
        System.out.println("This is Alpha");
    }
}
class Beta extends Alpha
{
    void show1()
    {
        System.out.println("This is Beta");
    }
}
class Gamma extends Beta
{
    void show2()
    {
        System.out.println("This is gamma");
    }
}
class AlphaBetaGamma
{
    public static void main(String[] args)
    {
        System.out.println("==================");
        Gamma g=new Gamma();
        g.show();
        g.show1();
        g.show2();
        System.out.println("==================");
    }
}
OutPut:

AlphaBetaGamma-on-multilple-inheritance-in-java.jpeg

Program:2 

class Tenth
{
    void schoolName()
    {
        System.out.println("Brilliant E.M School");
    }
    void marksGain()
    {
        System.out.println("Marks Secured in Tenth 490");
    }
}
class Inter extends Tenth
{
    void interClgName()
    {
        System.out.println("Sri sai krupa Jr College");
    }
    void marksGain1()
    {
        System.out.println("Marks Secured in Inter 586");
    }
}
class Degree extends Inter
{
    void degreeClgName()
    {
        System.out.println("Sri Sai Krupa Degree Collge");
    }
    void marksGain2()
    {
        System.out.println("Marks Secured in Degree 80.1");
    }
}
class Education
{
    public static void main(String[] args)
    {
        Degree d=new Degree();
        d.schoolName();
        d.marksGain();
        d.interClgName();
        d.marksGain1();
        d.degreeClgName();
        d.marksGain2();
    }
}

OutPut:

Multiple-Inheritance-Program-in-java.jpeg
 

Thursday, July 04, 2019

Single Inheritance In Java

Single Inheritance In Java


Definition


If any class is deriving from the another class, it can be called as single inheritance. In this inheritance these exists single base class and single derived class.

Example


single-Inheritance-in-Java.jpeg
In the above example, when we defined the java program on inheritance concept first we can create Base class (parent class) and later create derived class(child class). 
In oops concepts we can use and calls the one class features into another class by using inheritance which is shown below syntax. 

Syntax

class A
{
---------
---------

class B extends A
{
--------
--------



Note:- whenever we call the based class features into derived class then we must defined the keyword   called as extends Other wise compile time error. 

Q. Write a java program which will illustrate the concepts of single inheritance.

Program
class Alpha
{
    void show()
    {
        System.out.println("Hellow Alpha");
    }
}
class Beta extends Alpha
{
    void show1()
    {
        System.out.println("Hellow Beta");
    }
}
class Test
{
    public static void main(String[] args)
    {
        Beta b=new Beta();
        b.show();
        b.show1();
    }
}

Output
Alpha-beta-program-In-java,jpeg

Program: 2

class School
{
    void schoolInfo()
    {
        System.out.println("Brilliant School From Dhatmavaram");
    }
}
class Inter extends School
{
    void interClgInfo()
    {
        System.out.println("Sri Sai Krupa Jr College From Dhatmavaram");
    }
}
class SingleInheritance
{
    public static void main(String[] args)
    {
        System.out.println("---------------");
        Inter i=new Inter();
        i.schoolInfo();
        i.interClgInfo();
        System.out.println("---------------");
    }
}
OutPut:-
Single-Inheritanc-Program-In-Java.jpeg