Posts

Showing posts with the label Inheritance

Program to demonstrate use of hierarchical inheritance using interface

This is simple program for hierarchical inheritance using interface. Here an interface  Area  is created having data member  pi  and method  compute(float, float) . After that two classes are created  Rectangle  and  Circle . In these classes we have implemented  Area  interface and override the  compute(float, float)  method as shown in program. PROGRAM interface Area { final static float pi=3.14f; float compute(float x,float y); } class Rectangle implements Area { public float compute(float x,float y) { return(x*y); } } class Circle implements Area { public float compute(float x,float y) { return(pi*x*y); } } public class InterfaceTest { public static void main (String[] args) { Rectangle rect = new Rectangle(); Circle cir = new Circle(); Area area; area = rect; System.out.println("Area of Recatngle = "+area.compute(10,20)); area = cir; System.out.println("Area of Circle = "...

Program to implement the Multiple Inheritance (Gross Interface, Employee & Salary classes)

Image
In this program, we have achieved multiple inheritance using interface. We have created an interface  Gross  having data members  ta, da  and method  gross_sal( ) . After that class  Employee  is created which has data members  name, basic_sal  and method  display( ) . Finally we declare the  Salary  class having data member  hra  and method  disp( ) . In that we have extended  Employee  class and implemented  Gross interface as shown in program. PROGRAM import java.lang.*; // import language package interface Gross // interface declaration { double ta=800.0; double da=1500.0; // final variable void gross_sal(); } class Employee // super class declaration { String name; // data member declaration float basic_sal; Employee(String n, float b) // super class constructor { name=n; basic_sal=b; } void display() // method to display value of data member { System...

Program to implement the Multiple Inheritance (Bank Interface, Customer & Account classes)

In this program, we have achieved multiple inheritance using interface. We have created an interface  Bank  having data members  rate, no_of_years  and method  show( ) . After that class  Customer  is created which has data members  cust_name, cust_id  and method  display( ) . Finally we declare the  Account  class having data members  acc_no, acc_bal  and method  interest( ) . In that we have extended  Customer class and implemented  Bank  interface as shown in program. PROGRAM /* Program to implement the Multiple Inheritance */ interface Bank { float rate = 12.0f; int no_of_years=3; void show(); } class Customer { String cust_name; int cust_id; Customer(String n,int i) { cust_name = n; cust_id = i; } void display() { System.out.println("Customer Name = "+cust_name); System.out.println("Customer Id = "+cust_id); } } class Account extends Customer implements ...

Program to implement the Multiple Inheritance (Exam Interface, Student & Result classes)

Image
Interface looks like class but it is not a class. An  interface  can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default. The interface in java is  a mechanism to achieve fully abstraction . The class that implements interface must implement all the methods of that interface. Also, java programming language does not support multiple inheritance, using interfaces we can achieve this as a class can implement more than one interfaces. It cannot be instantiated just like abstract class. To achieve multiple inheritance in Java you have to use interface. The syntax for declaring interface is given below: interface <interface-name> { // variable declarations // Method declarations (no implementation) } The  implements  keyword is used to for implementing interface and...

Program to create a interface 'Mango' and implement it in classes 'Winter' and 'Summer'

This program demonstrate you the hierarchical inheritance in Java which contains one interface  Mango  and the other two subclasses  Summer  and  Winter  implement this interface using  implements  keyword. The syntax for declaring interface is given below: interface <interface-name> { // variable declarations // Method declarations (no implementation) } The  implements  keyword is used to for implementing interface and  extends  keyword is used to extend the class. The syntax for extending class and implementing interface is: class <subclass-name> extends <superclass-name> implements <interface-name> { // Body of subclass } You cannot create an object of interface but you can create a reference of it. Like, Mango m; PROGRAM //Creating interface 'Mango' having display( ) method. interface Mango { void display(); } //Implementing interface 'Mango'. class Summer implements Ma...

Program to create a interface 'printable' and implement it in 'InterfaceDemo' class

Interface looks like class but it is not a class. An  interface  can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default. The interface in java is  a mechanism to achieve fully abstraction . The class that implements interface must implement all the methods of that interface. Also, java programming language does not support multiple inheritance, using interfaces we can achieve this as a class can implement more than one interfaces. It cannot be instantiated just like abstract class. To achieve multiple inheritance in Java you have to use interface. The syntax for declaring interface is given below: interface <interface-name> { // variable declarations // Method declarations (no implementation) } The  implements  keyword is used to for implementing interface and  ...

Program to use inheritance to extend Box class

Like methods, constructors can also be overloaded.  Constructor overloading  is way of having more than one constructor which does different tasks. For e.g. Vector class has 4 types of constructors. If you do not want to specify the initial capacity and capacity increment then you can simply use default constructor of Vector class like this Vector v = new Vector( ); In this program, we have created a class  Box1  which have four constructors one is default and other three are parameterized constructors. Then we extended the class  Box1  in  BoxWeight1 . At last we have created object of  BoxWeight1  and called the method  volume( ) . PROGRAM //This program uses inheritance to extend Box. class Box1 { double width; double height; double depth; // construct clone of an object Box1(Box1 ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; } // constructor used when all dime...

Program to do the following Hierarchical Inheritance

In  Hierarchical Inheritance  there is one super class with many subclasses. Here, we have created one superclass  Shape2  which is declared as abstract because of one abstract method  area( ) . This superclass is get inherited into two subclasses,  Rectangle2  and  Triangle2  which then override this  area( )  method independently and do there own implementation in it. You cannot create an object of abstract class but you can create a reference of it as shown below. PROGRAM abstract class Shape2 { abstract void area(); } class Rectangle2 extends Shape2 { int length, breadth; Rectangle2(int l, int b) { length = l; breadth = b; } public void area() { System.out.println("Area of Rectangle : "+ (length * breadth)); } } class Triangle2 extends Shape2 { int side1, side2; Triangle2(int s1, int s2) { side1 = s1; side2 = s2; } public void area() { System.out.println("Area of Triang...

Program to implement Method Overriding for following inheritance

When a superclass and subclass both have a method having same name and same type signatures (same return type and number of arguments with same data types) then the method in the subclass is said to be  override  the method of superclass. In this program  area( )  is a method of superclass  Shape  which have same method name, same return type and no arguments is overridden in subclasses  Rectangle  and  Triangle . When a overridden method is called from within a subclass then it always calls the method in subclass, whereas the method defined in superclass will be hidden. An  abstract class  is a class that cannot be instantiated. When a method is declared as "abstract" in a class it must be overridden by the subclass of the super classes which contain that method. An  abstract method  does not have body. Body of an abstract method will be defined by subclass. To declare an  abstract method  use the follo...

Program to implement the following Multi Level Inheritance

In  multilevel inheritance  the subclass is super class to another class. The syntax for multilevel inheritance is given below: class <superclass-name> { // Body of superclass } class <subclass-name1> extends <superclass-name> { // Body of subclass } class <subclass-name2> extends <subclass-name1> { // Body of subclass } In this program, we have created three classes from which  Account  is a superclass for  Saving_Acc  and the  Saving_Acc  is the superclass for  Acct_Details . In main( ) method we have created an object Acct_Details and pass the values to constructor then called the display( ) method. Here we used the  super  keyword to call parent class constructor and method. Note that after compiling the  MultiLevelInh.java  file the four .class files will be created "Acoount.class", "Saving_Acc.class", "Acct_Details.class" and "MultiLevelInh.class". PROGRAM import ...

Program to show simple example of inheritance

Inheritance  is the property in which objects of one class acquires the properties of other class. This is the one of feature Java which is known as  Reusability  .With the use of inheritance, the information is made manageable in a hierarchical order. A class that is inherited is called a  superclass . The class that does the inheriting is called  subclass . It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements. There are following types of Inheritance: Single Inheritance (only one superclass and subclass) Multilevel Inheritance (subclass is super class of another class) Hierarchical Inheritance (one super class with many subclasses) Multiple Inheritance (several super classes for a subclass) Hybrid Inheritance (combination of above three types) Java supports the first three types of inheritance. Java does not support Multiple inheritance and hybrid inheritance. Java does not directly impl...

Program to demonstrate Single Inheritance

Inheritance  is the property in which objects of one class acquires the properties of other class. This is the one of feature of Java known as  Reusability . With the use of inheritance, the information is made manageable in a hierarchical order. A class that is inherited is called a  superclass . The class that does the inheriting is called  subclass . It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements. There are following types of Inheritance: Single Inheritance (only one superclass and subclass) Multiple Inheritance (several super classes for a subclass) Hierarchical Inheritance (one super class with many subclasses) Multilevel Inheritance (subclass is super class of another class) Hybrid Inheritance (combination of above four types) Java supports the first three types of inheritance. Java does not support Multiple inheritance and hybrid inheritance. Java does not directly implement the mu...