Unit 3 Inheritance
Unit 3 Inheritance
• The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
• In the terminology of Java, a class which is inherited is called a parent or superclass, and
the new class is called child or subclass.
Java Inheritance Example
class A {
protected String msg="Try to access the protected variable outside the class within the package";
}
public class ProtectedExample2 {
public static void main(String[] args) {
A a=new A();
System.out.println(a.msg);
}
}
Object class in Java
• The Object class is the parent class of all the classes in java by default. In other words, it
is the topmost class of java.
• Let's take an example, there is getObject() method that returns an object but it can be of
any type like Employee,Student etc, we can use Object class reference to refer that object.
For example:
Object obj=getObject();
• The Object class provides some common behaviors to all the objects such as object can be
compared, object can be cloned, object can be notified etc.
• The Object class provides many methods. They are as follows:
Method Description
returns the Class class object of this object. The
public final Class getClass() Class class can further be used to get the metadata
of this class.
public int hashCode() returns the hashcode number for this object.
public boolean equals(Object obj) compares the given object to this object.
protected Object clone() throws creates and returns the exact copy (clone) of this
CloneNotSupportedException object.
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
Understanding the real scenario of Abstract class
abstract class Shape Public class TestAbstraction1
{ {
abstract void draw();
} public static void main(String args[
class Rectangle extends Shape ])
{ {
void draw()
{
Shape s=new Circle1();
System.out.println("drawing rectangle");
} s.draw();
} }
class Circle1 extends Shape }
{
void draw()
{
System.out.println("drawing circle");
}
}
Interface in Java
• An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
• The interface in Java is a mechanism to achieve abstraction. There can
be only abstract methods in the Java interface, not method body. It is
used to achieve abstraction and multiple inheritance in Java.
• Java Interface also represents the IS-A relationship.
• It cannot be instantiated just like the abstract class.
Why use Java interface?
There are mainly three reasons to use
interface. They are given below.
•It is used to achieve abstraction.
•By interface, we can support the
functionality of multiple inheritance.
•It can be used to achieve loose coupling.
How to declare an interface?
}
FINAL METHODS AND CLASSES.
Final variable, Method and Class
1) final variable
• final variables are nothing but constants.
• We cannot change the value of a final variable once it is initialized. Lets have a look at the below code:
class Demo
{
final int MAX_VALUE=99;
void myMethod()
{
MAX_VALUE=101;
}
public static void main(String args[])
{
Demo obj=new Demo();
obj.myMethod();
}}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field Demo.MAX_VALUE cannot be assigned
2) Blank final variable
• a constructor is a special method used to initialize objects of a class. It has the same name as the
class, doesn't have a return type (not even void), and is automatically called when an object is
created using the new keyword.
Bike(){
speedlimit=70;
System.out.println(speedlimit);
}