Posts

Showing posts from July, 2019

Java Interface Example

Image
Java interface An  interface in java  is a blueprint of a class. It has static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body. Why use Java interface? There are mainly three reasons to use interface. They are given below. It is used to achieve abstraction. By interface, we can support the functionality of multiple inheritance. It can be used to achieve loose coupling. interface  Printable {   void  print();   }   interface  Showable {   void  show();   }   class  A  implements  Printable,Showable {   public   void  print() { System.out.println( "Hello" ); }   public   void  show() { System.out.println( "Welcome" ); }

Abstract class in Java With Example programs

Image
Def : Abstraction  is a process of hiding the implementation details and showing only functionality to the user. Abstract class in Java A class which is declared as abstract is known as an  abstract class . It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated. Points to Remember An abstract class must be declared with an abstract keyword. It can have abstract and non-abstract methods. It cannot be instantiated. It can have constructors and static methods also. It can have final methods which will force the subclass not to change the body of the method. Abstract class If a class contain any abstract method then the class is declared as abstract class. An abstract class is never instantiated. It is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method. Syntax : abstract class class_name { } Abstract method

Rules for Method Overriding and Example Program

Image
Rules for Method Overriding The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass. The access level cannot be more restrictive than the overridden method's access level. For example: If the superclass method is declared public then the overridding method in the sub class cannot be either private or protected. Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.

Hierarchical Inheritance in java with example

Image
Hierarchical Inheritance : In this  inheritance   multiple classes inherits from a  single class i.e there is one super class and  multiple   sub classes. As we can see from the below diagram when a same class is having more than one sub class (or) more than one sub class has the same parent is called as  Hierarchical Inheritance . public class ClassA { public void dispA () { System . out . println ( "disp() method of ClassA" ); } } public class ClassB extends ClassA { public void dispB () { System . out . println ( "disp() method of ClassB" ); } } public class ClassC extends ClassA { public void dispC () { System . out . println ( "disp() method of ClassC" ); } } public class ClassD extends ClassA { public void dispD () { System . out . println ( "disp() method of ClassD" ); } } public class Hierar

Multilevel Inheritances in Java Program

Image
Multilevel inheritances in Java In Multilevel inheritances there exists single base class, single derived class and multiple intermediate base classes. Single base class + single derived class + multiple intermediate base classes. Intermediate base classes An intermediate base class is one in one context with access derived class and in another context same class access base class. class Car { public void Car() { System.out.println("Class Car"); } } class Maruti extends Car { public void Maruti() { System.out.println("Class Maruti"); } public void brand() { System.out.println("Brand: Maruti"); } } public class Inherit extends Maruti { public void speed() { System.out.println("Max: 80Kmph"); } public static void main(String args[]) { Inherit obj=new Inherit(); obj.brand(); obj.speed(); obj.Car(); obj.Maruti(); } }

SINGLE INHERITANCE :

Image
SINGLE INHERITANCE : The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class. class Shape { int length ; int breadth ; } public class Rectangle extends Shape { int area ; public void calcualteArea () { area = length * breadth ; } public static void main ( String args []) { Rectangle r = new Rectangle (); //Assigning values to Shape class attributes r . length = 10 ; r . breadth = 20 ; //Calculate the area r . calcualteArea (); System . out . println ( "The Area of rectangle of length \"" + r . length + "\" and breadth \"" + r . breadth + "\" is \"" + r . area + "\"" ); } }

Inheritance Notes

Image
INHERITANCE : Inheritance in Java  is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the  IS-A relationship  which is also known as a parent-child relationship.         Why use inheritance in java For Method Overriding (so runtime polymorphism can be achieved). For Code Reusability. The  extends keyword  indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality. In the terminology of Java, a class which is inherited is called a parent or superclass, and the new