MRS N SUBATHRA ASST PROFESSOR DEPARTMENT OF COMPUTER
MRS. N. SUBATHRA ASST. PROFESSOR DEPARTMENT OF COMPUTER SCIENCE
v Inheritance is a mechanism wherein a new class is derived from an existing class. Super Class Sub Class v A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass.
v To inherit a class in JAVA by using the keyword extends in the definition of one class to another. v Example: class B extends A Derived class Base Class
TYPES OF INHERITANCE
EXAMPLE OF SINGLE INHERITANCE Class A{ public void method. A(){ System. out. println("Base class method"); }} Class B extends A{ public void method. B(){ System. out. println("Child class method"); } public static void main(String args[]) { B obj = new B(); obj. method. A(); //calling super class method obj. method. B(); //calling local method }}
MULTILEVEL HIERARCHY v When a class extends a class, which extends another class then this is called multilevel inheritance
SYNTAX Class A { //statements; } Class B extends A { //statements; } Class C extends B { //statements; public static void main(String args[]) { //statements; } }
MULTIPLE INHERITANCE v Multiple Inheritance is a feature of object oriented concept, where a class can inherit properties of more than one parent class.
MULTIPLE INHERITANCE IS NOT POSSIBLE Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. One of the example of such problem is the diamond problem. .
DIAMOND PROBLEM Class B and Class C are inherited from Class A , Class D is extending both B & C so if D wants to use a method the overridden method of B or the overridden method of C is called which causes Ambiguity. That’s the main reason why Java doesn’t support multiple inheritance.
TO ACHIEVE MULTIPLE INHERITANCE The Multiple Inheritance can be achieved only through interfaces. v Classes can implement more than one interfaces but can not extend more than one class. v
HYBRID INHERITANCE v It is a mix of two or more of the above types of inheritance. Since java doesn't support multiple inheritance with classes, the hybrid inheritance is also not possible with classes.
ACHIEVING HYBRID INHERITANCE v In java, we can achieve hybrid inheritance only through Interfaces.
MEMBER ACCESS Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are − v Visible to the package, the default. No modifiers are needed. v Visible to the class only (private). v Visible to the world (public). v Visible to the package and all subclasses (protected).
ACCESS CONTROL AND INHERITANCE The following rules for inherited methods are enforced − v Methods declared public in a superclass also must be public in all subclasses. v Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private. v Methods declared private are not inherited at all, so there is no rule for them.
VISIBILITY
SUPER CLASS VARIABLE REFERENCING SUBCLASS OBJECT v A reference variable of a superclass can be used to a refer any subclass object derived from that superclass. v When a reference to a subclass object is assigned to a superclass reference variable, we can access only the parts of object defined by the superclass. v Example
class Base{ public void display() { System. out. println("Base class display method is called"); }} class Derv 1 extends Base { public void display(){ { System. out. println("Derv 1 class display method is called”); } } class derv 2 extends Base { public void display() { System. out. println("Derv 2 class display method is called"); } } class polymorhism { public static void main(String[] args) { Base ptr; Derv 1 dl = new Derv 1(); Derv 1 d 2 = new Derv 1(); ptr = dl; ptr. display(); ptr = d 2; ptr. display(); } }
USING SUPER v The super keyword in java is a reference variable that is used to refer parent class objects. Has two general forms: 1. Calls the superclass constructor 2. Used to access a member of the superclass that has been hidden by a member of a subclass.
USING SUPER TO CALL SUPERCLASS CONSTRUCTORS v We shall execute the constructor of a super class by using the super keyword in a simple command of the following form : super (parameter-list); where, parameter-list specifies any parameters needed by the constructor in the superclass.
USING SUPER TO CALL SUPERCLASS MEMBERS v We shall execute the member of a super class by using the super keyword in a simple command of the following form : super. member Member can either a method or an instance variable.
EXAMPLE Class Superclass { int num = 100; } Class Subclass extends Superclass{ int num = 110; void print. Number(){ System. out. println(num); } public static void main(String args[]) { Subclass obj= new Subclass(); obj. print. Number(); } }
THE USE OF SUPER KEYWORD 1) To access the data members of parent class when both parent and child class have member with same name 2) To explicitly call the no-argument and parameterized constructor of parent class. 3) To access the method of parent class when child class has overridden that method.
NOTE v The super() is the first statement that in the constructor because the superclass constructors are to be initialized first before the object is given to the subclass. v The super() can used only in constructors and not in ordinary methods.
QUESTIONS AND ANSWERS 1. Can a class extend itself. ? No, A class can not extend itself. 2. What happens if both, super class and sub class, have a field with same name. ? Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword. 3. Are static members inherited to sub classes? Yes, Static members are also inherited to sub classes.
CONTD. . 4. Can a private members of a class are inherited to sub class? No. Private members cannot be inherited. 5. Which class is a default super class for every class in java? java. lang. Object class 6. Does java support multiple inheritance? Yes, but only through interfaces. Classes can implement more than one interfaces but can not extend more than one class.
IMPORTANT QUESTIONS
- Slides: 29