0% found this document useful (0 votes)
22 views

Unit 3

Inheritance and Polymorphism document discusses key object-oriented programming concepts in Java including inheritance, method overriding, interfaces, packages, and access protection. Inheritance allows classes to inherit features from other classes in a hierarchy. Method overriding and dynamic dispatch allow subclasses to provide their own implementation of methods while interfaces define behaviors without implementations. Packages organize related classes and can hide classes from other packages. Access protection determines whether classes and members can be accessed from other classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Unit 3

Inheritance and Polymorphism document discusses key object-oriented programming concepts in Java including inheritance, method overriding, interfaces, packages, and access protection. Inheritance allows classes to inherit features from other classes in a hierarchy. Method overriding and dynamic dispatch allow subclasses to provide their own implementation of methods while interfaces define behaviors without implementations. Packages organize related classes and can hide classes from other packages. Access protection determines whether classes and members can be accessed from other classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Inheritance and Polymorphism

• Inheritance: Inheritance in Java, Creating


Multilevel hierarchy, Constructors in derived
class,
• Method overriding, Dynamic method dispatch.
• Interfaces: Define, implement and extend,
Accessing Interface variables, Default interface
• methods, Using static method in interface
• Packages: Java API Packages, Using System
Packages, Creating accessing and using a
• package, Importing packages, Adding a class to a
Package, Hiding classes
Inheritance in Java
• the mechanism in java by which one class is
allowed to inherit the features(fields and
methods) of another class.
• In Java, inheritance means creating new
classes based on existing ones. A class that
inherits from another class can reuse the
methods and fields of that class.
• In addition, new fields and methods can be
added to current class as well.
Inheritance in Java
• Code Reusability: The code written in the
Superclass is common to all subclasses. Child
classes can directly use the parent class code.
• Method Overriding: Method Overriding is
achievable only through Inheritance. It is one of
the ways by which java achieves Run Time
Polymorphism.
• Abstraction: The concept of abstract where we
do not have to provide all details is achieved
through inheritance. Abstraction only shows the
functionality to the use
Terminologies used in Inheritance
• Class: Class is a set of objects which shares common
characteristics/ behavior and common properties/
attributes. Class is not a real-world entity. It is just a
template or blueprint or prototype from which objects are
created.
• Super Class/Parent Class: The class whose features are
inherited is known as a superclass(or a base class or a
parent class).
• Sub Class/Child Class: The class that inherits the other class
is known as a subclass(or a derived class, extended class, or
child class). The subclass can add its own fields and
methods in addition to the superclass fields and methods.
Inheritance
• The extends keyword is used for inheritance
in java.
• Syntax :
class derived-class extends base-class {
//methods and fields }
super keyword
• The super keyword is similar to this keyword.
– It is used to differentiate the members of
superclass from the members of subclass, if they
have same names.
– It is used to invoke the superclass constructor
from subclass.
• Differentiating the Members
– If a class is inheriting the properties of another
class. And if the members of the superclass have
the names same as the sub class, to differentiate
these variables super keyword is used.
• super.variable
• super.method();
class Animal{
String color="white";
}
class cat extends Animal{
String color="Black”;
void printColor(){
System.out.println(color);
System.out.println(super.color);
}
}
class TestSuper1{
public static void main(String args[]){
Cat c=new Cat();
c.printColor();
}}
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work(){
super.eat();
bark();
}
}
class TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
d.work();
}}
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();
}}
*eclipse examples
Types of Inheritance
• 1. Single Inheritance
– In single inheritance, subclasses inherit the
features of one superclass
Types of Inheritance(Cont..)
• 2. Multilevel Inheritance
– In Multilevel Inheritance, a
derived class will be inheriting
a base class, and as well as the
derived class also acts as the
base class for other classes.
Types of Inheritance(Cont..)
• 3. Hierarchical Inheritance
– In Hierarchical Inheritance, one class serves as a
superclass (base class) for more than one
subclass.
Inheritance
Multilevel Inheritance Ex
• Eclipse
Hierarchical Inheritance ex
Method Overriding and Dynamic
Method dispatch
• Method overriding is one of the ways in which
Java supports Runtime Polymorphism.
• Dynamic method dispatch is the mechanism by
which a call to an overridden method is resolved
at run time, rather than compile time.
• At run-time, it depends on the type of the object
being referred to (not the type of the reference
variable) that determines which version of an
overridden method will be executed
Method Overriding and Dynamic
Method dispatch(Cont)
class A{}
class B extends A{}
A a=new B();//upcasting
Method invocation is determined by the JVM
not compiler, it is known as runtime
polymorphism.
Method Overriding example
class Bike{
void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely ");}

public static void main(String args[]){


Bike b = new Honda();//upcasting
b.run();
}
}
Interfaces (Multiple Inheritance)
• Defines only abstract methods and final fields

• The Java compiler adds public and abstract keywords


before the interface method. It adds public, static and
final keywords before data members.

• Syntax
interface <interface_name>{

// declare constant fields


// declare methods that abstract
// by default.
}
interface Item
{
static final int code=24;
static final String name=“Agastya”
void display();
{
Extending Interfaces
• a class extends another class, an interface
extends another interface, but a class
implements an interface. A class uses
the implements keyword to implement an
interface.

Example
interface printable{
void print();
}
class printing implements printable{
public void print() {System.out.println("Hello");}

public static void main(String args[]){


printing obj = new printing();
obj.print();
}
}
Multiple Inheritance
Interface Inheritance
Example
Default method in Interface
• Before Java 8, interfaces could have only
abstract methods.
• The implementation of these methods has to
be provided in a separate class.
• Java 8 has introduced the concept of default
methods which allow the interfaces to have
methods with implementation without
affecting the classes that implement the
interface.
Default method ex

package com.Interface;
interface TestInterface1 public static void main(String[] args) {
{ // TODO Auto-generated method stub
// abstract method defaultmethod d = new
public void square(int a); defaultmethod();
d.square(4);
// default method
default void show()
{
// default method executed
System.out.println("Default Method Executed"); d.show();
} }
}
public class defaultmethod implements }
TestInterface1 {
public void square(int a)
{
System.out.println(a*a);
}
Static methods in interfaces
interface TestInterface class TestClass implements
TestInterface
{ {
// abstract method // Implementation of square
abstract method
public void square (int a); public void square (int a)
{
// static method System.out.println(a*a);
static void show(){ }
System.out.println("Static public static void main(String args[])
Method Executed"); {
TestClass d = new TestClass();
} d.square(4); // Static method
} executed
TestInterface.show();
}
The interfaces can have static methods as well
} which is similar to static method
of classes.
Packages

• Packages: Java API Packages, Using System


Packages, Creating accessing and using a
package, Importing packages, Adding a class
to a Package, Hiding classes
Package
• A group of similar types of classes, interfaces
and sub-packages
• Packages: built-in and user defined packages
Example
Accessing class property from another
package
Adding a class to a package
package p1;
public classA
{
//body of A
} //Adding a classA to p1 // A.java file

package p1;
public class B
{
//body of B
} //Adding class B in p1 // B.java file
Hiding classes
package p1;
public class x
{
//body of x
}
class Y
{
//body of y
}
import p1.*;
X objectX;
Y objectY;
Access Protection
Top Level class can be
public,default,abstract,final

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy