Professional Documents
Culture Documents
J2SE - Core Java - PG-DAC - Session 4v1
J2SE - Core Java - PG-DAC - Session 4v1
Session 4:
Learning Objectives
By the end of this session, you must be able to
Describe Inheritance Explain Method Overriding in classes Understand run-time polymorphism Use super keyword Explain Abstract Class Describe Interfaces
Inheritance
Inheritance
INHERITANCE is one of the pillars of object-orientation
A new class is derived from an existing class: 1) Existing class is called super-class 2) Derived class is called sub-class
A sub-class is a specialized version of its super-class: 1) has all non-private members of its super-class 2) may provide its own implementation of super-class methods Objects of a sub-class are a special kind of objects of a superclass
Class Hierarchy
A child class of one parent can be the parent of another child, forming class hierarchies At the top of the hierarchy theres a default class called Object
Animal
Reptile
Bird
Mammal
Snake
Lizard
Parrot
Horse
Bat
Class Object
In Java, all classes use inheritance.
If no parent class is specified explicitly, the base class Object is implicitly inherited.
All classes defined in Java, is a child of Object class, which provides minimal functionality guaranteed to e common to all objects.
Class Object
Methods defined in Object class are; 1. equals(Object obj) Determine whether the argument object is the same as the receiver 2. getClass() Returns the class of the receiver, an object of type Class 3. hashCode() Returns a hash value for this object. Should be overridden when the equals method is changed 4. toString() Converts object into a string value. This method is also often overridden 5. finalize() Called by the garbage collector on an object when garbage collectior determines that there are no more references to the object 6. clone() Creates and returns a copy of this object. 7. wait() 8. wait(long timeout) 9. wait(long timeout, int nanos) 10. notify() 11. notifyAll()
One baseobj=new One(); // base class object. Two subobj=new Two(); // child class object super class object baseobj can be used to refer its sub class objects. For example, Baseobj=subobj // now its pointing to sub class
Example
Example
class SuperClass{ void m1(){ System.out.println("Super Class m1"); } } public class SubClass extends SuperClass{ extends SuperClass { void m1(){ System.out.println("Sub Class m1"); } public static void main(String[] args) { SubClass sb=new SubClass(); Overriding the sb.m1(); method SuperClass sp=new SubClass(); sp.m1(); } }
abstract
class MyClass{ void m1(){ System.out.println("in MyClass m1"); } abstract void m2();
}
This class can be declared abstract even it does not have any abstract methods
abstract class MyClass{ void m1(){ System.out.println("in MyClass m1"); } void m2(){ System.out.println("in MyClass m1"); } An abstract can also have all abstract methods, but not so }
This class is having abstract class MyClass{ 2 Abstract abstract void m1(); methods abstract void m2(); void m3(){ System.out.println("in MyClass m3"); } AbstractTest should implement the 2 } abstract methods of MyClass
public class AbstractTest AbstractTest extends extends MyClass{ MyClass{ void m1(){ System.out.println("MyClass-m1 defined by AbstractTest"); } void m2(){ System.out.println("MyClass-m2 defined by AbstractTest"); } public static void main(String[] args) { MyClass mc=new AbstractTest(); mc.m1(); mc.m2(); mc.m3(); } }
Example
Interfaces
An interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types.
Uses of Interfaces
Interfaces
1. Contain only method declarations and public static final constants
Keyword interface Only have public members A class implementing an interface must implement all of the methods defined in the interface
2. 3. 4.
2.
3.
4.
A class extending an abstract class need not implement any of the methods defined in the abstract class
Next
Packages
Access Modifiers
Wrapper Classes