Inheritance in java

Extending a class: To inherit a class

class sub_class extends  base_class
{}

Every class is sub-class of object class (implicitly extending every  class we can use or extend its methods.).
object class : have following methods
  • protected Object clone() throws CloneNotSupportedException
  • public boolean equals(Object obj)
  • protected void finalize() throws Throwable
  • public final Class getClass()
  • public int hashCode(): reference of object 
  • public String toString()
  • ic final void notify()
  • public final void notifyAll()
  • public final void wait()
  • public final void wait(long timeout)
  • public final void wait(long timeout, int nanos)
private members of a super class is not accessible we can access using some methods .

Up-Casting:  its happen implicitly, this is like widening conversion of primitive types 
super class_object = sub class object.
super class object can access only super class methods. super class object dont have any idea what its sub class is doing. 


Down-Casting : We have to do explicitly,narrowing conversion :

sub class object = ( sub class)super class object 

Member hiding:  If sub-class have same name of a method as super class then it hide the method of super class, so using method name we can call only subclass method. To call super class method.

super keyword: a)  calling super class constructor ( every sub class constructor implicitly have super as its first line inside  constructor )
b) accessing a hide member of super class  : super.method_name

Constructors are called in mlti-level hierarchy 


Method overriding: Subclass have same signature of a method as in its super class then method is call override of super class method.  overriding is call for non-static and non private methods. 
While overriding we cannot decrease the visibility of a method.

Abstract class: Canot directly be instantiated we have to extend it and then only call its methods. abstract class have constructor if not defined explicitly then then there id implicit construct.
static methods of a class can be call directly with class name.     



  

Comments

Popular posts from this blog

Introduction to Java

Creational Design Pattern: Factory Design Pattern

Design Patterns: The Secret Weapon of Expert Developers