Full Transcript

Object-Oriented Programming with Java (OOPJ) DU # 2302CS304 Unit-4 Inheritance and Abstraction Prof. Pranami V. Sanja Department of Computer Engineering Darshan Institute of Engineering & Te...

Object-Oriented Programming with Java (OOPJ) DU # 2302CS304 Unit-4 Inheritance and Abstraction Prof. Pranami V. Sanja Department of Computer Engineering Darshan Institute of Engineering & Technology for Diploma Studies, Darshan University, Rajkot [email protected] Topics  Looping Method Overloading OOP Java is the easiest, scoring and my favorite subject Constructor Constructor Overloading this and static keyword Inheritance finalize method Method Overriding super and final keyword Abstract class Interface Dynamic Method Dispatch OOP Java is the easiest, scoring and my favorite subject Method Overloading Section 1 Method Overloading  A class have multiple methods with same name, but different parameters is known as Method Overloading. OOP Java is the easiest, scoring and my favorite subject  It is also known as compile time (static) polymorphism.  Overloading of methods with different return types is not allowed.  Overloading Methods must be differ in at least one of the following:  Number of parameters  Data type of parameters  Order of parameter Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 4 Method Overloading (Cont..) Example: class MOverLDemo { public static void main(String args[]) OOP Java is the easiest, scoring and my favorite subject void sum(int a, int b) { { MOverLDemo o1 = new MOverLDem System.out.println("Sum of (a+b) is:: o1.sum(10,10); "+(a+b)); } o1.sum(10,10,10); void sum(int a, int b, int c) o1.sum(10.5,10.5); { } System.out.println("Sum of (a+b+c) is:: "+(a+b+c)); } } Output: void sum(double a, double b) Sum of (a+b) is:: 20 { Sum of (a+b+c) is:: 30 System.out.println("Sum of double (a+b) is:: Sum of double (a+b) is:: 21.0 "+(a+b)); } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 5 OOP Java is the easiest, scoring and my favorite subject Constructor Section 2 Constructor  Constructor is special type of method that is used to initialize the object. OOP Java is the easiest, scoring and my favorite subject  Constructor is invoked automatically whenever an object of class is created.  Constructor name must be same as class name.  Constructors do not have return types and they cannot return values, not even void.  There are two type of constructor: 1) Default Constructor 2) Parameterized Constructor Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 7 Default Constructor  Constructor without parameter is known as default constructor.  If we don’t explicitly declare a constructor for any class, the compiler OOP Java is the easiest, scoring and my favorite subject creates default constructor for that class. Example: class A Default Constructor { Output: A() { Default constructor is called.. System.out.println("Default constructor is called.."); } public static void main(String args[]) { A a = new A(); } } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 8 Parameterized Constructor  Constructor with parameter is known as parameterized constructor.  It is required to pass parameters at the time of object creation. OOP Java is the easiest, scoring and my favorite subject Example: class A { int a; Parameterized Constructor public static void main(String args[ ]) String s1; { A(int b, String str) A a = new A(10,"Hello"); { a.display(); a = b; } s1 = str; } } void display() Output: { Value of a & s1 is:: 10 Hello System.out.println("Value of a & s1 is:: "+a+" "+s1); } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 9 Copy Constructor  A copy constructor is used to create another object that is a copy of the object that it takes as a parameter. OOP Java is the easiest, scoring and my favorite subject  But, the newly created copy is totally independent of the original object. Example: class Student { String name; int rollno; Student(String s_name,int s_roll) { System.out.println("Constructor is Invoked"); name=s_name; rollno=s_roll; } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 10 Copy Constructor (Cont..) Example: (Cont..) Student(Student s) { Student s1=new OOP Java is the easiest, scoring and my favorite subject System.out.println("CopyConstructor Invoked"); Student("DU",101); name=s.name; Student s2=new Student(s1); s1.display(); rollno=s.rollno; s2.display(); } } public void display() { } System.out.print("name="+name); Output: System.out.println("rollno="+rollno); Constructor is Invoked } } Copy Constructor is Invoked class DemoCopy { public static void main(String[] args) { name=DU rollno=101 Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 11 OOP Java is the easiest, scoring and my favorite subject Constructor Overloading Section 3 Constructor Overloading  It allows to take more than one constructor inside one Class.  It is same as method overloading, but in constructor overloading OOP Java is the easiest, scoring and my favorite subject doesn’t have return type.  Example: class Balance { int accNo; double bal; Balance() { System.out.println("Constructor 1"); bal=0; } Balance(double b) { System.out.println("Constructor 2"); bal=b; } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 13 Constructor Overloading (Cont..)  Example (Cont.): Balance(int a,double b) System.out.println("b1.bal="+b1.bal); OOP Java is the easiest, scoring and my favorite subject { System.out.println("b2.bal="+b2.bal); System.out.println("Constructor 3"); bal=b; System.out.println("b3.bal="+b3.bal); accNo=a; } } } } Output: class Demo Constructor 1 { Constructor 2 public static void main(String args[]) { Balance b1= new Balance(); Constructor 3 Balance b2= new Balance(100); Balance b3=new Balance(1201,10000); b1.bal=0.0 Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 14 OOP Java is the easiest, scoring and my favorite subject this keyword Section 4 this Keyword  this is always a reference to the object on which the method was invoked.  It is used to hiding the variable. OOP Java is the easiest, scoring and my favorite subject  We cannot create two Instance/Local variables with same name. But, it is legal to create one instance variable & one local variable.  Local Variable will hide the instance variable which is called Variable Hiding. public static void main(String args[]) Example: Instance { class A Variable A a1 = new A(); { a1.method(); int v = 5; } Local Output: void method() } Variable Value of Instance variable :5 { Value of Local variable :40 int v = 40; System.out.println("Value of Instance variable :" + this.v); System.out.println("Value of Local variable :" + v); Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 16 OOP Java is the easiest, scoring and my favorite subject static keyword Section 5 static Keyword  Method and variable can be declared as static.  When a member is declared static, it can be accessed before any objects OOP Java is the easiest, scoring and my favorite subject of its class are created.  We can call a static method from outside its class: classname.method();  Several restrictions when any method declare as static:  It can only call other static methods.  It must only access static data.  It cannot refer to this or super in any way. Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 18 static Keyword Example: class staticDemo public static void main(String args[]) OOP Java is the easiest, scoring and my favorite subject { { static int count = 0; staticDemo s1 = new staticDemo(); staticDemo() staticDemo s2 = new staticDemo(); { count++; staticDemo s3 = new staticDemo(); System.out.println(count); display(); } } static } { Output: System.out.println("Static block initialized..."); Static block initialized... } 1 static void display() 2 { 3 System.out.println("Static method called…"); Static method call… } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 19 OOP Java is the easiest, scoring and my favorite subject Inheritance Section 6 Inheritance  Inheritance is the process by which class can acquire the properties and methods OOP Java is the easiest, scoring and my favorite subject of its parent class.  Deriving new child class from old parent class is known as inheritance.  New class is known as Derive Class.  Old parent class is known as Base Class.  Types of Inheritance  Single Inheritance  Multilevel Inheritance  Multiple Inheritance  Hierarchical Inheritance  Hybrid Inheritance Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 21 Inheritance Types of Inheritance Multilevel Inheritance Single Inheritance Multiple Inheritance Hierarchical Inheritance OOP Java is the easiest, scoring and my favorite subject A A C B A B B A C B C  Hybrid Inheritance A C B  A Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 22 Single Inheritance Example: class A { OOP Java is the easiest, scoring and my favorite subject public void displayA() class DemoSingle { { public static void System.out.println("Method of class A"); main(String args[]) } { } A a = new A(); class B extends A { a.displayA(); public void displayB() B b= new B(); { b.displayA(); System.out.println("Method of class B"); b.displayB(); } } } } Output: Method of class A Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & AbstractionMethod of class A 23 Multilevel Inheritance Example: class C extends B { public void displayC() { class A { OOP Java is the easiest, scoring and my favorite subject System.out.println("Method of public void displayA() class C"); { } } System.out.println("Method of class A"); class DemoMultilevel { } public static void main(String } args[]) { C c = new C(); class B extends A { c.displayA(); public void displayB() c.displayB(); { c.displayC(); } System.out.println("Method of class B"); } } Output: } Method of class A Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 24 Hierarchical Inheritance Example: class C extends A { public void displayC() { class A { OOP Java is the easiest, scoring and my favorite subject System.out.println("Method of public void displayA() class C"); { } } System.out.println("Method of class A"); class DemoHierarchical { } public static void main(String } args[]) { B b = new B(); class B extends A { b.displayA(); public void displayB() b.displayB(); { C c = new C(); c.displayA(); System.out.println("Method of class B"); c.displayC(); } } } } Output: Method of class A Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 25 OOP Java is the easiest, scoring and my favorite subject finalize method Section 7 finalize() method  finalize() is the method of Object class.  This method is called just before an object is garbage collected. OOP Java is the easiest, scoring and my favorite subject  finalize() method overrides to dispose system resources, perform clean-up activities and minimize memory leaks. Example: protected void finalize() public class Demo { { System.out.println("finalize method called"); public static void main(String[] args) { } Demo obj = new Demo(); } System.out.println(obj.hashCode()); Output: obj = null; // calling garbage collector 1159190947 System.gc(); System.out.println("end of garbage collection"); end of garbage collection } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 27 OOP Java is the easiest, scoring and my favorite subject Method Overriding Section 8 Method Overriding  The subclass has the same name and type signature as method in its superclass, then the method in the subclass is said to override the method OOP Java is the easiest, scoring and my favorite subject of the super class.  Method overriding is used for runtime polymorphism. Rules for Method Overriding:  Child class method and parent class method have same argument list and return type.  Final method cannot be override.  Constructors cannot be override.  Subclass (Same package) – can override non private and non final methods.  Subclass (Different package) – Can override only public or protected methods. Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 29 Method Overriding (Cont.) Example: class A class DemoMethodOverriding OOP Java is the easiest, scoring and my favorite subject { { public void display() public static void main(String { args[]) { System.out.println("Class A method"); } A a = new A(); } A b = new B(); class B extends A a.display(); { b.display(); @Override } public void display() } Output { Class A method System.out.println("Class B method"); Class B method } } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 30 Method Overloading v/s Method Overriding v/ Method Overloading Method Overriding s OOP Java is the easiest, scoring and my favorite subject Overloaded methods must The argument list must exactly change the argument list. match that of the overridden Overloaded methods can change method. The return type must be the the return type. same as overridden method in Private and final methods can be the super Private andclass. final methods cannot overloaded. be overridden. It is performed within class. It occurs in two classes which performs inheritance. Static binding is being used for Dynamic binding is being used overloaded methods. for overridden methods. Method overloading is the Method overriding is the example of compile time example of run time polymorphism. polymorphism. Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 31 OOP Java is the easiest, scoring and my favorite subject super keyword Section 9 Super Keyword  super is reference variable.  It is used to refer immediate parent class object. OOP Java is the easiest, scoring and my favorite subject  It is used to access super class member as well as super class constructor. Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 33 Super Keyword (Cont.) super To Access super-class Members void printName() Example: { OOP Java is the easiest, scoring and my favorite subject class A System.out.println("Subclass  : "+na { System.out.println("Superclass: "   String name = "Class A"; + super.name);  public void display() display();  { super.display();  System.out.println("Class A method called"); } } } } Output class DemoSuperKeyword class B extends A Subclass : Class { B { Superclass: Classpublic A static void main(String args[ ]) Class B method {  String name = "Class B";  public void display() called B b1 = new B(); { Class A method b1.printName(); System.out.println("Class called B method called"); } } } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 34 Super Keyword (Cont.) class B extends A super To call super-class Constructor { Example: B() OOP Java is the easiest, scoring and my favorite subject class A { { System.out.println("Subclass  A() default constructor called" { System.out.println("Super-class default constructor called"); } } B(String  s1)  A(String s1) { { super("Class System.out.println("Super-class parameterized  A"); System.out.println("Subclass constructor called: "+ s1); }  parameterized constructo } called: " + s1); class DemoSuperConstructor } { } public static void main(String args[ ]) Output { Super-class default constructor called  B b1 = new B(); B b2 = new B("Class B"); Subclass default constructor called  Super-class parameterized constructor called: } } Class A Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 35 OOP Java is the easiest, scoring and my favorite subject final keyword Section 10 final Keyword  A variable, method & class can be declared as final.  The final variable act as constant and its value cannot be changed. OOP Java is the easiest, scoring and my favorite subject  If method declared as final then it cannot be override.  If class declared as final then it cannot be inherit. Example: public static void main(String class A { args[]) { final int b = 100; A a1 = new A(); void method() a1.method(); { } } b = 200; // Error !! We can not change Output: the value of final variable Compilation error } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 37 OOP Java is the easiest, scoring and my favorite subject Abstract class Section 11 Abstract Class  A class that is declared with abstract keyword is known as an abstract class. OOP Java is the easiest, scoring and my favorite subject  It consist abstract as well as non-abstract methods.  A method that is declared as abstract and does not have implementation is known as abstract method.  Abstract method needs to be extended and its method implemented.  We can’t create object of abstract class.  If any class extends abstract class, that class must implement all abstract methods of super class : Syntax: abstract class class_name { //Number of abstract as well as non-abstract methods. } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 39 Abstract Class Example: abstract class A public static void main(String[] args) OOP Java is the easiest, scoring and my favorite subject { { abstract void abs_method(); DemoAbstract abs = new public void display() DemoAbstract(); abs.abs_method(); { abs.display(); System.out.println(“Concrete method called"); } } } } Output class DemoAbstract extends A An abstract method { called void abs_method() Concrete method { called System.out.println(“An abstract method called"); } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 40 OOP Java is the easiest, scoring and my favorite subject Interface Section 12 Interface  An interface is a collection of abstract methods.  It is not a class. OOP Java is the easiest, scoring and my favorite subject  By default, an interface is abstract, data members are public, static and final and methods are public and abstract.  It is mechanism to achieve fully abstraction and implement multiple inheritance in java: Example: Syntax: interface DemoInterface public interface NameOfInterface { { int i = 10; Abstract Method mea //Any number of final, static fields void demo(); Only declaration withou //Any number of abstract method declarations } body. } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 42 Interface Implement Interface  A class uses the implements keyword to implement an interface. OOP Java is the easiest, scoring and my favorite subject  It means class as signing a contract, agreeing to perform the specific behaviors of the interface.  If a class does not perform all the behaviors of the interface, the class must declare itself as abstract. Output Example: Value of i is :: public class InterfaceImp implements DemoInterface 10 { public void demo() public static void main(String args[]) { { System.out.println("Value of i is :: "+i); InterfaceImp d = new InterfaceIm } d.demo(); } } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 43 Interface Inheritance of Interface class InheritInterface implements B  An interface can extend another {interface. OOP Java is the easiest, scoring and my favorite subject String display;  It is work like a class extend another class. public void setdata(String name)  Example: { interface A display = name; } { public  void getdata()  void setdata(String name); { } System.out.println(display); interface B extends A } { public static void main(String args[])  void getdata(); { } InheritInterface obj = new InheritInterfac obj.setdata("Welcome To Heaven"); Output obj.getdata(); Welcome To } Heaven } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 44 Interface Multiple Inheritance class InheritInterface implements A, B using Interface { OOP Java is the easiest, scoring and my favorite subject  If a class implements multiple String display; public void setdata(String name) interfaces or an interface extends  { multiple interfaces known as display = name; multiple inheritance. } Example: public  void getdata() public  interface A { { System.out.println(display); void setdata(String name); } } public static void main(String args[]) public interface B {  InheritInterface obj = new InheritInterfac { obj.setdata("Welcome To Heaven"); Output void getdata(); obj.getdata(); Welcome } To } Heaven } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 45 Class v/s Interface v/ Class Interface s OOP Java is the easiest, scoring and my favorite subject You can instantiate class. You cannot instantiate an It contains default as well as interface. It does not contain any parameterize constructors. constructors. All the methods should have All the methods in an interface definition otherwise declare are abstract by default. method as abstract explicitly. All the variables are instance by All the variables are static final default. by default and a value needs to be assigned at the time of A class can inherit only one definition. An interface cannot inherit any Class and can implement many class while it can extend many interfaces. interfaces. Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 46 Interface v/s Abstract Class v/ Interface Abstract Class s OOP Java is the easiest, scoring and my favorite subject Interface can have only abstract Abstract class can have abstract methods. and non-abstract methods. Interface supports multiple Abstract class doesn't support inheritance. multiple inheritance. Interface can't have static Abstract class can have static methods, main method or methods, main method and constructor. Interface can't provide the constructor. Abstract class can provide the implementation of abstract implementation of interface. class. Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 47 OOP Java is the easiest, scoring and my favorite subject Dynamic Method Dispatch Section 13 Dynamic Dispatch Method  It is mechanism by which overridden method is resolved at runtime rather than OOP Java is the easiest, scoring and my favorite subject compile time.  It is known as Runtime Polymorphism. class C extends A { Example: void callme() class A { { System.out.println("Inside C's callm void callme() method"); { } System.out.println("Inside A's callme } method"); } } class B extends A { void callme() { System.out.println("Inside Prof. Pranami V Sanja B's callme method"); #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 49 Dynamic Dispatch Method Example: (Cont..) Output class DemoDynamicDispatch Inside A's OOP Java is the easiest, scoring and my favorite subject callme { method public static void main(String args[]) { Inside B's callme A a = new A(); method B b = new B(); Inside C's callme C c = new C(); method Reference variable r of A r; type A r = a; r.callme(); r = b; r.callme(); r = c; r.callme(); } } Prof. Pranami V Sanja #2302CS304(OOPJ) Unit 04 – Inheritance & Abstraction 50 Object Oriented Programming with JAVA (OOPJ) DU # 2302CS304 Thank You Prof. Pranami V Sanja Department of Computer Engineering Darshan Institute of Engineering and Technology for Diploma Studies, Rajkot. [email protected]. in

Use Quizgecko on...
Browser
Browser