A subclass inherits all of the public and protected members of its parent. The inherited members can be used in the following ways:
- The inherited fields can be used directly, just like any other fields.
- You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it
- The inherited methods can be used directly as they are.
- You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
- You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
- You can write a subclass constructor that invokes the constructor of the superclass, wither implicitly or by using the keyword super
Casting of Objects
A parent class object can be assigned with a derived class object. This is called implicit casting
Example:
    Object obj = new Rectangle();
If a derived class is assigned with a parent class, a compile-time error should occur. To avoid this, explicit casting is used.
Example:
    Rectangle rectOne = (Rectangle)obj;
Overriding Methods
When overriding a superclass method in the subclass, the @Override annotation can be used, to instruct the compiler that you want to override the method in the subclass. The access specifier for an overriding subclass method should allow more access, than the overriden superclass method. That is, a protected instance method in the superclass can only be made public but not private, in the subclass.
'super' Keyword
A superclass field is hidden by a subclass field of same name, even if their types are different. To access the superclass field, super keyword is used. Similarly, overriden methods in the superclass, can be invoked from the subclass using the keyword super. Constructor Chaining is a concept in which, a subclass constructor invokes a superclass constructor, either implicitly or explicitly and the chain goes all the way back to the object class constructor.
Examples:
    super.superClassMethod();
    super.superClassField = someVariable;
    super( parameter list ); - constructor of superclass
Methods in Object class
Any classes inherit object class methods. See Object class API specification to check the list of methods. The methods should be overriden according to the requirement in the derived class.
- protected Object clone() throws CloneNotSupportedException
Creates and returns a copy of this object. clone() can be invoked in a class that implements Cloneable interface. Else, the method throws a CloneNotSupportedException. Without any exceptions, the clone() method creates the object of the same class as the original object and initializes the new object's member variables to have the same values as the original object's corresponding member variables. - public boolean equals(Object obj)
Compares two objects for equality and returns true if they are equal. Generally, identity operator (==) is used to determine equality for objects of primitive datatypes. For objects of reference datatypes, the equals() method, checks if the object references are equal or not. - protected void finalize() throws Throwable
To free-up resources, when an object becomes garbage. - public final class getClass()
Returns a Class object, which is used to get information, such as its name (getSimpleName()), its superclass(getSuperClass()), and the interfaces it implements(getInterfaces()). The getClass() method cannot be overriden. - public int hashCode()
Returns a hash code value of the object, which is the objects memory address in hexadecimal. By definition, when 2 objects are equal, their hash code must also be equal.
A method can be defined as final to prevent it from being overriden in the subclass. A whole class can be declared as final, thus preventing it from being subclassed. String is an example of final class.
Abstract Classes and Methods
An Abstract class is a class that is declared abstract and it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can only subclassed.
Syntax:
    abstract ClassName{ Class Definition }
An abstract method is a method, declared without an implementation and ends with a semicolon.
Syntax:
    abstract returntype methodName(parameters);
All the methods in an interface are implicitly abstract.
Unlike interfaces, abstract classes can contain fields that are not static and final and also methods that have definition. An interface is an abstract class that contains only abstract method declarations. Unlike other classes, abstract classes can implement an interface without implementing all the interface's methods.
tutorials
No comments:
Post a Comment