Java - Unit 2.1
Java - Unit 2.1
Java - Unit 2.1
Inheritance
Inheritance can be defined as 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.
The class which inherits the properties of other is known as subclass (derived class, child
class) and the class whose properties are inherited is known as superclass (base class,
parent class).
class Super {
.....
}
class Sub extends Super {
.....
}
Example
In case of overridden methods always subclass method will be executed unless super
is invoked.
All other methods defined only in sub class cannot be executed
The reason for this is: In compile time, the check is made on the reference type
(My_Calculation()). However, in the runtime, JVM figures out the object type and would
run the method that belongs to that particular object.
Types of
inheritance
Single level inheritance
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Multilevel Inheritance Example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
Output:
void bark(){System.out.println("barking...");} weeping...
} barking...
class BabyDog extends Dog{ eating...
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Hierarchical
class Animal{ inheritance
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark()
{System.out.println("barking...");}
}
class Cat extends Animal{ Output:
void meow() meowing...
eating...
{System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
Why not multiple inheritance
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
2) super can be used to invoke parent class method
The super keyword can also be used to invoke parent class method. It should be used if
subclass contains the same method as parent class. In other words, it is used if method
is overridden.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
Output:
void bark(){System.out.println("barking...");}
void work(){
eating...
super.eat();
barking...
bark();
}
}
class TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
d.work();
}}
3. super is used to invoke parent class
constructor.
The super keyword can also be used to invoke the parent class
constructor. Let's see a simple example:
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
Output:
animal is created
Access modifier
Modifier Description
package defaultPackage;
class Logger {
void message(){
System.out.println("This is a message");
}
}
class Data {
// private variable
private String name;
}
public class Main {
public static void main(String[] main){
// create an object of Data
Data d = new Data();
// access private variable and field from another class
d.name = "Programiz"; //error
}
}
class Data {
private String name;
// getter method
public String getName() {
return this.name;
}
// setter method
public void setName(String name) {
this.name= name;
}
}
public class Main {
public static void main(String[] main){
Data d = new Data();
class Animal {
// protected method
protected void display() {
System.out.println("I am an animal");
}
}
class Dog extends Animal {
public static void main(String[] args) {
// create an object of Dog class
Dog dog = new Dog();
// access protected method
dog.display();
} }
Abstraction in Java
Abstraction is a process of hiding the implementation details from the user, only the
functionality will be provided to the user. In other words, the user will have the
information on what the object does instead of how it does it.
Abstract classes may contain abstract methods, i.e., methods without body
( public void get(); ) or normal methods.
But, if a class has at least one abstract method, then the class must be declared
abstract.
If a class is declared abstract, it cannot be instantiated.
To use an abstract class, you have to inherit it from another class, provide
implementations to the abstract methods in it.
If you inherit an abstract class, you have to provide implementations to all the
abstract methods in it.
It can have constructors and static methods also.
abstract class Bike{
abstract void run();
void run1(){
System.out.println("running not safely..");}
}
Java allows to write more than one method in the same class definition with the same name
For example, two methods in ShoppingCart class can have the name computeCost.
3) By anonymous object:
new Employee();
Methods:
finalize() method
The finalize() method is invoked each time before the object is garbage
collected. This method can be used to perform cleanup processing. This method
is defined in Object class as:
protected void finalize(){}
gc() method
The gc() method is used to invoke the garbage collector to perform cleanup
processing. The gc() is found in System and Runtime classes.
public static void gc(){}
public class TestGarbage1{
public void finalize(){System.out.println("object is garbage collected");}
public static void main(String args[]){
TestGarbage1 s1=new TestGarbage1();
TestGarbage1 s2=new TestGarbage1();
s1=null;
s2=null;
System.gc();
}
}
Output:
object is garbage collected
object is garbage collected
Interfaces
It has a collection of abstract methods. A class implements an interface, thereby
inheriting the abstract methods of the interface. interface contains behaviors that
a class implements.
An interface may also contain constants, static methods but Method bodies exist
only for static methods. An interface can contain any number of methods.
Unless the class that implements the interface is abstract, all the methods of the
interface need to be defined in class.
An interface is different from a class in several ways, including −
You cannot instantiate an interface.
All of the methods in an interface are abstract and are implicitly public.
An interface cannot contain instance fields. The only fields that can appear in
an interface must be declared both static and final.
An interface can extend multiple interfaces.