Saturday, April 13, 2019

Implementing Inheritance In Java

Understanding Inheritance


Inheritance is one of the most important features of Object Oriented Programming (OOP).The class that is used to create a new class is called a base class, superclass or parent class.The newly created class is called subclass,child class or derived class.



Depending on the number and nature of class, inheritance can be classified in to 3 types.

  1. Single Inheritance
  2. Hierarchical Inheritance
  3. Multilevel Inheritance                     



Implementing Single Inheritance



Single inheritance is the most common type of inheritance.It is simply creating a derived class from a single base class.It represents a linear relationship between a superclass and its subclass.

Let's  consider the mysub and mysuper classes,which are a subclass and a superclass.

---  Listing 1 : mysuper class

//declaring super class
      
class mysuper
 {
   public void msgfromsuper()
 {
  System.out.println("In super class");
  }
---- Listing 2 : mysub class    
  
//declaring subclass  

class mysub  extends mysuper 
{
public void msgfromsub()
{
    System.out.println("In sub class");
}
}

---- Listing 3 : main class

public class anu
{
public static void main(String args[])
{
 mysub d =new mysub();
d.msgfromsub();
d.msgfromsuper();
}
}



Program 

Consider an employee class, which contains fields such as name and desg. and a subclass, which contains a field sal. Write a program for inheriting this relation.



Implementing Hierarchical Inheritance


The next form of inheritance is Hierarchical inheritance, in which more than one subclass is derived from a single superclass.

Eg.

Consider two classes, car and aircraft , are derived from vehicle class.

--- Listing superclass and subclass

class a
{
public void display()
{
System.out.println("Hi shanu");
}
}
class b extends a
{
public void display1()
{
System.out.println("how are you");
}
}
class c extends a
{
public void display2()
{
System.out.println("okay");
}

--- Listing main class

class anu
{
public void main(String args[ ])
{
a cv = new a( );
cv.display2;
cv.display1;
}




Implementing Multilevel inheritance


---Listing subclass and superclass

class a
{
public void display( )
{
System.out.println("hii");
}
}
class b extends a
{
public void display1( )
{
System.out.println("i am anu");
}
}
class c extends b
{
public void display2( )
{
System.out.println("i am fine");

---Listing Main class

public class anu
{
public void main(String args[ ])
{
c s = new c ( );
s.display 1( );
s.display 2( );
s.display ( );
}
}

program

Eg:

Write a program for implementing multilevel inheritance




Referencing Subclass Objects

program

Write a program to access the super class reference in its subclass.



         Using The Super Keyword


The super keyword is used to access the members of a superclass in a sub class.

Eg.

--- Listing  the super keyword

class ani
{
int value = 300;
}
class anu extends ani
{
int value = 400;
public void show()
{
System.out.println(" From the subclass:"+ super.value);
System.out.println("From the super class:"+ value);
}
}
public class cv
{
public void main(String args[ ])
{
anu e = new anu( );
e.show( );
}
}

Program

Eg:

write a program to display the use of the super keyword.





Using The Final Keyword

When the final keyword is used with a variable, the final variable is declared as a constant and you cannot modify its value. Similarly, if reference variable is declared as final, then its reference cannot be changed. In addition, when it is used with a method , then method cannot be overridden in a subclass. Moreover, using the final keyword with a class prevents the class from being inherited.

Eg :

class anu
{
final void display()
{
System.out.println("Invoking superclass method");
}
}
class shanu extends anu
{
public void display( )
{
System.out.println("Invoking  subclass method");
}
}
public class vv
{
public static void main(String args[ ])
{
System.out.println("invoking members");
shanu c =new shanu( );
c.display;
}
}




The final display ( ) method of the super class is overridden the subclass, which produces an error message.



Declaring Abstract Classes

Eg:

write a program to compute the areas of a rectangle and square by using an abstract class


abstract class anu
{
int length;
int breadth;
abstract int area( );
}

class rectangle extends anu
{
int area( )
{

return length * breadth;
}
}
class square  extends anu
{
int area( )
{
length=breadth;
return  length*breadth;
}
}

public class si
{
public static void main(String args[ ])
{
System.out.println("area of square");
square s = new square( );
s.length=8;
s.breadth=7;
s.area( );
System.out.println(s.area());

System.out.println("area of  rectangle");
rectangle r = new rectangle( );
r.length=6;
r.breadth=4;
r.area( );
System.out.println(r.area());

}
}




Implementing Interface in Java

Eg:

Write a program to calculate the area by using an interface


interface cal
{
public int area( );
}
class rectangle implements cal
{
int length=0;
int breadth=0;
public int area( )
{
return length*breadth;
}
}
class square implements cal
{
int length =0;
int breadth=0;
public int area( )
{
length=breadth;
return length*breadth;
}
}
public class g
{
public static void main(String args[ ])
{
System.out.println(" area of square");
square q = new square( );
q.length =3;
q.breadth=3;
q.area();
System.out.println(q.area());
System.out.println(" area of rectangle");
rectangle v = new rectangle();
v.length=8;
v.breadth=3;
v.area();
System.out.println(v.area());
}
}






Extending Interface

An interface can extend another interface by using the extends keyword.

Eg:

Write a program for demonstrating interface extension


interface one
{
public void messg1();

}
interface two extends one
{
public void messg2();
}

class sha implements two
{
public void messg1()
{
System.out.println("Executing super interface");
}

public void messg2()
{
System.out.println("Executing sub interface");


}
}
public class g
{
public static void main(String[] args) 
{
vivi k =new vivi();
k.messg2();
sha h =new sha();
h.messg1();
}
}





Exception Handling


Exceptions are certain abnormal conditions or errors that occur at runtime and can cause an abrupt termination of a program.

Eg.

Write a program, which can raise arithmetic exception

class anu
{
public static void main(String args[ ]);
try{
      int a =7;
      int b=0;
      int z=a/b;
      System.out.println("Result of the programm is "+z);
      }
catch(Exception e)
{
e.printStackTrace( );
System.out.println("A number cannot be divided by z);
}
}
}




Eg.

Write a program to handle an ArrayIndexOutofBoundsException in a java program.



class f
{
public static void main(String args[ ])
try{
        int  val[ ] ={1,3,2,4};
        int x;
        for(x=0;x<=4;x++)
{
System.out.println(val[x]);
}
}
catch(Exception e)
{
e.printStackTrace( );
System.out.println("no fourth element in the array");
}
}
}





Using Finally Clause

Write a program to demonstrate the finally clause

public class anu
{
public static void main(String args[ ])
{
try{
int a =9;
int b=8;
int square =0;
square = a*b;
System.out.println(square);
}
catch(ArithmeticException a)
{
a.printStackTrace( );
}
finally{
System.out.println("finally block must be executed");
}
}
}





 Threads


Write a program to create and use a thread

class anu implements Runnable
{
public void run( )
{
for(i=0; i<=5; i++)
{
System.out.println(" Thread");
}
}
public class ann
{
public static void main(String args[ ])
{
anu u = new anu( );
Thread v = new Thread(u);
v.start( );
}
}





Write a program to create and use multiple threads


class anu implements Runnable
{
public void run( )
{
System.out.println("Current thread" + Thread.currentThread( ).getName( ) );
}
}
public class b
{
public static void main(String args[ ] );
{
anu r = new anu( );
Thread t = new Thread(r);
t.start(r);
Thread t1 =new Thread(r);
t1.start( );
Thread t2 = new Thread(r);
t2.start( );
}
}



Write a program to get the priority of a thread


class anu implements Runnable
{
public void run( )
{
System.out.println("current Thread"+ Thread.currentThread( ).getName( ));
}
}
public class f
{
public static void main(String args[ ] )
{
anu i = new anu( );
Thread t = new Thread(i);
t.start( );
System.out.println(t.getPriority( ));
}
}






Sleeping and Waking Up

Write a program to demonstrate the use of the sleep() method.

class anu implements Runnable
{
public void run( )
{
for(int i=1;i<=5;i++)
{
System.out.println("Current value"+i);
try{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Thread interrupted");
}

}
}
}
public class e
{
public static void main(String[] args)
 {
anu l = new anu();
Thread t = new Thread(l);
t.start();

}

}



Joining

Write a program to demonstrate the use of the
 join
( ) method


class n implements Runnable
{
public  void run()
{
for(int i=1; i<=5;i++)
{
System.out.println("The current value ="+i);
}
System.out.println("child Terminated");
}
}
public class e
{
public static void main(String[] args)
 {
n k = new n();
Thread t = new Thread(k);
t.start();
try{
t.join();

}
catch(InterruptedException e){}
System.out.println("Main Terminated");

}

}









If else statement in java ( with examples) - Java World

In this article, you will learn to use two selection statements: if and if else to the statements which alter the flow of  your program’s ...