M3 Inheritance Copy
M3 Inheritance Copy
• OUTPUT:
• eating...
• barking...
A Second Use for super is used to refer immediate parent
class instance variable.
The second form of super acts somewhat like this, except that it always refers to the superclass of
the subclass in which it is used. This usage has the following general form: super.member
member can be either a method or an instance variable.
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();
}}
Creating a Multilevel Hierarchy
class A {
void funcA() {
System.out.println("This is class A"); } }
class B extends A {
void funcB() { System.out.println("This is class B");
} }
class C extends B { void funcC() {
System.out.println("This is class C"); } }
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
obj.funcC();
} }
When Constructors Are Executed
• // Create a super class.
class A {
A() { System.out.println("Inside A's constructor."); } }
// Create a subclass by extending class A.
class B extends A {
B() { System.out.println("Inside B's constructor."); } }
// Create another subclass by extending B.
class C extends B {
C() { System.out.println("Inside C's constructor."); } }
• class CallingCons {
• public static void main(String[] args) {
• C c = new C();
• }
• }
• Output:
• The output from this program is shown here:
• Inside A's constructor
• Inside B's constructor
• Inside C's constructor
• Note:the constructors are executed in order of derivation.
• Overriding is a feature that allows a subclass or child class to provide a
specific implementation of a method that is already provided by one
of its super-classes or parent classes.
• Method overriding occurs only when the names and the type
signatures of the two methods are identical.
• Method overriding is one of the ways by which Java achieves
Run Time Polymorphism.
Java program to demonstrate method overriding in java
// Base Class
class Parent {
void show()
{ System.out.println("Parent's show()"); } }
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
@Override void show() {
System.out.println("Child's show()"); } }
// Driver class
class Main { public static void main(String[] args) {
// If a Parent type reference refers // to a Parent object, then Parent's //
show is called
Parent obj1 = new Parent();
obj1.show(); // If a Parent type reference refers // to a Child object Child's
show() // is called. This is called RUN TIME // POLYMORPHISM.
Parent obj2 = new Child(); obj2.show(); } }
Create three sub classes
namely: circle, triangle and square, each class has two
member functions named
draw () and erase (). Demonstrate polymorphism concepts
by developing suitable
methods, defining member data and main program.
• class Shape { // Member functions
• public void draw()
• { System.out.println("Drawing a shape"); }
• public void erase() {
• System.out.println("Erasing a shape"); }}
• // Circle class, a subclass of Shape
• class Circle extends Shape {
• public void draw1() { System.out.println("Drawing a circle"); }
• public void erase1() { System.out.println("Erasing a circle"); }}
• // Triangle class, a subclass of Shape
• class Triangle extends Shape {
• public void draw2() { System.out.println("Drawing a triangle"); }
public void erase2() { System.out.println("Erasing a triangle"); }}
• class Square extends Shape{
• public void draw3() {
• System.out.println("Drawing a square"); }
• public void erase3() { System.out.println("Erasing a square"); }}
• // Main program to demonstrate polymorphism
• class Main{
• public static void main(String[] args) {
• // Creating objects of different shapes
• Circle c = new Circle();
• Triangle t = new Triangle();
• Square s = new Square(); // Demonstrating polymorphism by calling
//draw and erase methods
• System.out.println("Using Circle object:"); c.draw(); c.erase();
System.out.println("\nUsing Triangle object:"); t.draw(); t.erase();
System.out.println("\nUsing Square object:"); s.draw(); s.erase(); }}
OUTPUT
• Using Circle object:
• Drawing a shape
• Erasing a shape