Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Cse 3rd year.

Similar presentations


Presentation on theme: "Inheritance Cse 3rd year."— Presentation transcript:

1 Inheritance Cse 3rd year

2 class display { private void display_1 (){ System.out.println("private_display"); } public void display_2(){ System.out.println(“public_child"); display_1(); // only this way class test{ public static void main(String []args){ display ds=new display (); ds.display_2(); ds.display_1(); // wrong

3 What is inheritance? Inheritance is the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

4 Ex:Object class: equals(), toString(), hashcode(),Arrays.copyOf()

5 Inheritance is an important pillar of OOP(Object Oriented Programming)
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of more than one class. Important terminology: Super Class: The class whose features are inherited is known as super class(or a base class or a parent class). Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods. Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

6 Not allowed

7

8 Private member cant be inherited
class parent { private int i; private String st; public void display_parent (){ System.out.println("Parent"+i+st);} private void display1_parent (){ System.out.println(“PrivateParent"+i+st);} } public class HelloWorld1 extends parent{ private void display_child_1 (){ System.out.println("child_display_1"); } public void display_child_2 (){ System.out.println(" display_child_2 "); display_child_1(); public static void main(String []args){ HelloWorld1 hw=new HelloWorld1(); hw.display_child_2(); hw.display1_parent(); // wrong Private member cant be inherited

9 Prototype of inheritance of various architecture:
child extends parent{ ……. Single level inheritance ………… } child1 extends parent{ child2 extends parent{ // Hiererchical child3 extends parent{ child1 extends parent1{ child2 extends child1 { // multilevel parent child parent child 1 child 2 child 3 parent1 child1 child2

10 class Student extends Person{ // inheritance
class Person{ Person(){ System.out.println("This is Person Constructor");} public void message1(){ System.out.println("This is person class");}}  class Student extends Person{ // inheritance Student(){ // System.out.println("This is student class"); // WRONG super should be first line super(); // now you can add your code } public void message(){ //super(); // only in Student Constructor super.message1(); System.out.println("This is student class"); } Public void display(){ super.message1(); } class Test { public static void main(String args[]){ Student s = new Student(); s.display();} } No problem

11 class Child extends Parent{ Child(){ super(); super(Parameters);
The super keyword, is a reference variable which refers immediate parent class objects.  Syntex: super()->constructor; super.method(),super.method(parameters), super.variable Rules for using super: super() must be the first line in child class constructor but super.method() not necessary to be first line. class Child extends Parent{ Child(){ super(); super(Parameters); super.Parent_yui(); super. Parent_yui(Parameters); System.out.println(“you can write your code….");} } child class constructor can call super.method(parameters) but, no method can call any super(parameters or no parameters). Any one of should stay Not problem

12 System.out.println("Person1 class Constructor “+y); }
class Person1 { Person1(int y) { System.out.println("Person1 class Constructor “+y); } public void yui() { System.out.println("yui_Person1"); } } // end of Person1 class class Person extends Person1{ Person(){ super(5); super.yui();} public void yui(){ System.out.println("yui_Person"); super.yui();

13 Inner class Outer.inner in_temp=in; in_temp.display_inner();
public class Outer { class Inner{ private int i; String st; public void display_inner (){ System.out.println("inner_class"+i+st); } public static void main(String []args){ Outer out=new Outer(); Outer. Inner in=out.new Inner(); // same Outer. Inner in=new Outer().new Inner(); in.display_inner(); // inner_class 0 null Outer.inner in_temp=in; in_temp.display_inner();

14 In general, Outer.inner.inner_1.inner_2……inner_n in_n= new Outer().new Inner(). new Inner_1(). new Inner_2()…….Inner_n(); in_n.display_inner_n();

15 public void display_parent_inner (){
Sample code: class parent { class parent_inner { private int i; String st; public void display_parent_inner (){ System.out.println("Parent_inner"+i+st); } } parent parent_inner Child public class child extends parent public static void main(String[]args) { child.parent_inner in=ch.new parent_inner(); in.display_parent_inner(); }

16 Static inner class Case 1: outer class---->static inner class---->static inner class HelloWorld12.inner.inner1 in1=new HelloWorld12.inner.inner1(); in1.display_inner1(); Case 2: outer class---->static inner class----> inner class HelloWorld12.inner.inner1 in1=new HelloWorld12.inner().new inner1(); Case 3: outer class----> inner class----> static inner class (wrong as locally static is not allowed). “Static class cant be outer class”

17 Function overriding: prototype needs to be same
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super- class. “for override public to protected not allowed but reverse is fine” weaker access specifier is assigned when we make public to protected that is not abide by java.

18

19 Dynamic polymorphism(DP)
Method overriding is one of the ways to use Dynamic method ,by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called through a superclass reference, JVM determines which version(superclass/subclasses) of that method is to be invoked based upon the type of subclass object. At run-time, it depends on the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed A superclass reference variable can refer to a subclass object. This is also known as upcasting. Java uses this fact to resolve calls to overridden methods at run time. If we don’t know the runtime reference then use DP.

20 Parent1 pr1=new child1(); Parent1 pr1=new child2();
Child1 ch1=new child2(); Child c=new Child(); Parent p=new Child(); If we know a prior what type of object is to be used If we don’t know This can show own, inherited and overridden members Only own and overridden that present in child class

21 class Person { protected String st_person="DP_Person"; protected void Experiment_overrid(){ s.o.p("yui_Person: st_person"+st_person);} public void dis_person(){ System.out.println(“show:"+st_person);} } class Person1 extends Person { protected String st_person1=“DP_Person1"; protected void Experiment_overrid (){ System.out.println("overrriden by: Person1"); } protected void dis_person1 (){ System.out.println(“Show:"+st_person+st_person1);} class Person2 extends Person1 protected String st_person2=“DP_Person2"; System.out.println("overrriden by: Person2"); } protected void dis_person2 (){ System.out.println(“show:"+st_person+st_person1+st_person2);} Person pr=new Person2(); pr. Experiment_overrid(); // work pr. dis_person2 (); // not work Person1 pr=new Person2(); pr. dis_person1 (); // work Person pr=new Person1(); pr. dis_person (); // work 3) pr.dis_person1(); // not work Note that, you need to use separate class along with extends Person2.

22 Abstract class Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only essential things to the user and hides the internal details, for example, implementing a Scientific calculator where only functionality is mentioned. Implementation about the this type of class is left for subclasses. Abstraction lets you focus on what the object does instead of how it does it.

23 1. An abstract class may or may not include abstract methods. 2
1. An abstract class may or may not include abstract methods. 2. Abstract classes cannot be instantiated, but they can be subclassed. 3. An abstract class may have final, static fields and static methods. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract. “Abstract class partially deals with overriding not completely”

24 abstract class Person {
abstract protected void abc(); abstract protected void abc(int x,int y); public void dis_person(){ System.out.println(“show: dis_person”); }} abstract class Person1 extends Person{ protected or public void abc(){ System.out.println(“override abc of Person”); } abstract protected void abc(int x,int y); // yet to defined next class public void dis_person1(){ System.out.println(“dis_person1”);}} class Person2 extends Person1{ System.out.println(“again override abc of Person2”); protected void abc(int x,int y){ System.out.println(“dis_person2 x=”+x+”y=”+y);}} public class HelloWorld2_abstarct extends Person2{ public static void main(String []args){ Person1 pr1=new HelloWorld2_abstarct(); pr1.abc(); pr1.abc(5,6); Person2 pr2=new HelloWorld2_abstarct(); pr2.abc(); HelloWorld2_abstarct hwa=new HelloWorld2_abstarct(); hwa.abc(7,8); hwa.abc();}} DP

25 Miscellaneous example:
String type StringBuffer Integer User defined anything al Object ob=al.get(i);

26 Multiple inheritance (interface)


Download ppt "Inheritance Cse 3rd year."

Similar presentations


Ads by Google