0% found this document useful (0 votes)
3 views36 pages

Unit 4

Digital marketing

Uploaded by

Land lord
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views36 pages

Unit 4

Digital marketing

Uploaded by

Land lord
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Inheritance

Inheritance allows a software developer to


derive a new class from an existing one
The existing class is called the parent class,
or super class, or base class
The derived class is called the child class or
subclass
As the name implies, the child inherits
characteristics of the parent
Inheritance
That is, the child class inherits the methods
and data defined by the parent class
We can refer to these inherited methods and
variables as if they were declared locally in
the class
Inheritance
Inheritance relationships are shown in a UML
class diagram using a solid arrow with an unfilled
triangular arrowhead pointing to the parent class

Vehicle

Car

• Proper inheritance creates an is-a relationship,


meaning the child is a more specific version of the
parent
Inheritance
A programmer can tailor a derived class as
needed by adding new variables or methods, or
by modifying the inherited ones
Software reuse is a fundamental benefit of
inheritance
By using existing software components to
create new ones, we capitalize on all the effort
that went into the design, implementation, and
testing of the existing software
Deriving Subclasses
In Java, we use the reserved word extends
to establish an inheritance relationship

class Car extends Vehicle


{
// class contents
}
Access Protction:

Java has four type of access specifier private, public, protected and
default . There are different access rules for all these access
specifier in inheritance.
 Private data member
private data member can only be accessed in same class where
they are declared. Child class don't have access to parent class
private data member.

class base
{
private int a;
}
class der extends base
{
void print()
{
System.out.println("a:"+a); // Error: private data can't be accessed.
}
}
 public data member
public data member of parent class can be accessed in
child class.

class base
{
public int a;
}
class der extends base
{
void print()
{
System.out.println("a:"+a);
}
}
Above program wil be compiled successfully.
protected data member
Protected data member are same as private
with one exception that they can be accessed in
child class.

class base
{
protected int a;
}
class der extends base
{
void print()
{
System.out.println("a:"+a);
}
}
Above program wil be compiled successfully.
Default data member

Default data member are those data


member which are not declared with any
access specifier. Default data member
works same as public in same package. So
default data member can be accessed in
child class
The protected Modifier
Visibility modifiers affect the way that class
members can be used in a child class
Variables and methods declared with private
visibility cannot be referenced by name in a
child class
They can be referenced in the child class if
they are declared with public visibility -- but
public variables violate the principle of
encapsulation
There is a third visibility modifier that helps
in inheritance situations: protected
The protected Modifier
The protected modifier allows a child class
to reference a variable or method directly in
the child class
It provides more encapsulation than public
visibility, but is not as tightly encapsulated
as private visibility
A protected variable is visible to any class in
the same package as the parent class
The protected Modifier
Protected variables and methods can be
shown with a hash ( # )symbol preceding
them in UML diagrams
NOTE:

All methods & variables (even those declared


private) are inherited by the child class
Their definitions exist and memory is reserved
for the variables
However they CANNOT be referenced by
name
Class Diagram for Words
Book
# pages : int
+ pageMessage() :
void

Words Dictionary
- definitions : int
+ main (args : String[]) :
void + definitionMessage() :
void
Constructor in inheritance
 When a class inherit another class then child class depends on
parent class data. It make sense that before initialising child class
object, parent class object need to be initialised. In java whenever
child class constructor is called, it make a call to parent class constr
 class one
{
one()
{
System.out.println("one class constructor");
}
}
class two extends one
{
two()
{
System.out.println("two class constructor");
}
public static void main(String arg[])
{
two t =new two();
}
}
/*Output:
one class constructor
two class constructor
*/
Super Keyword
The super keyword in java is a reference variable
that is used to refer immediate parent class object.
Whenever you create the instance of subclass, an
instance of parent class is created implicitly i.e.
referred by super reference variable.
Usage of java super Keyword
super is used to refer immediate parent class
instance variable.
super() is used to invoke immediate parent class
constructor.
super is used to invoke immediate parent class
method.
Super Keyword class B extends A
 super keyword is used to call { int b;
constructor of parent class. In B()
child class super keyword used {
to refer parent class. System.out.println("B's Default
constructor");
class A }
B(int y)
{ {
int a; super(y); // It will call parent class
A() parameterised constructor
b=y;
{ System.out.println("B's parameterised
System.out.println("A's Default constructor ");
constructor "); }
} public static void main(String arg[])
{
A(int x) B b1 =new B();
{a=x; B b2=new B(12)
System.out.println("A's parameterised }}
constructor "); /*Output:
} A's Default constructor
B's Default constructor
} A's parameterised constructor
B's parameterised constructor
*/
The super Reference
Constructors are not inherited, even though
they have public visibility
Yet we often want to use the parent's
constructor to set up the "parent's part" of
the object
The super reference can be used to refer to
the parent class, and often is used to invoke
the parent's constructor
The super Reference
A child’s constructor is responsible for
calling the parent’s constructor
If the child constructor invokes the parent
(constructor) by using the super reference, it

MUST be the first line of code of the


constructor
The super reference can also be used to
reference other variables and methods
defined in the parent class
class Person{
void message(){System.out.println("welcome");}
}
class Student extends Person{
void message(){System.out.println("welcome to java");}
void display(){
message();//will invoke current class message() method
super.message();//will invoke parent class message() method
}
public static void main(String args[]){
Student s=new Student();
s.display();
}
}
Output:welcome to java
welcome
Types of Inheritance
Single 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
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
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 Inheritance
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){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
}}
Multiple Inheritance
Java supports single inheritance, meaning that a
derived class can have only one parent class
Multiple inheritance allows a class to be derived
from two or more classes, inheriting the
members of all parents
Collisions, such as the same variable name in
two parents, have to be resolved
Java does not support multiple inheritance
In most cases, the use of interfaces gives us
aspects of multiple inheritance without the
overhead
Why multiple inheritance is not supported in java?
 To reduce the complexity and simplify the language, multiple inheritance is
not supported in java.
 Consider a scenario where A, B and C are three classes. The C class inherits
A and B classes. If A and B classes have same method and you call it from
child class object, there will be ambiguity to call method of A or B class.
 Since compile time errors are better than runtime errors, java renders
compile time error if you inherit 2 classes. So whether you have same method
or different, there will be compile time error now.

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

Public Static void main(String args[]){


C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
Overriding Methods
A child class can override the definition of an
inherited method in favor of its own
The new method must have the same
signature as the parent's method, but can
have a different body
The type of the object executing the method
determines which version of the method is
invoked
Overriding
A method in the parent class can be invoked
explicitly using the super reference
If a method is declared with the final
modifier, it cannot be overridden
The concept of overriding can be applied to
data and is called shadowing variables
Shadowing variables should be avoided
because it tends to cause unnecessarily
confusing code
Overloading vs. Overriding
Overloading deals with multiple methods with the
same name in the same class, but with different
signatures
Overriding deals with two methods, one in a
parent class and one in a child class, that have
the same signature
Overloading lets you define a similar operation in
different ways for different parameters
Overriding lets you define a similar operation in
different ways for different object types
Abstract Classes
An abstract class is a placeholder in a class
hierarchy that represents a generic concept
An abstract class cannot be instantiated

We use the modifier abstract on the class


public abstract class Product
header to declare a class as abstract:
{
// contents
}
Abstract Classes
An abstract class often contains abstract
methods with no definitions (like an
interface)
Unlike an interface, the abstract modifier
must be applied to each abstract method
Also, an abstract class typically contains
non-abstract methods with full definitions
A class declared as abstract does not have
to contain abstract methods -- simply
declaring it as abstract makes it so
Abstract Classes
The child of an abstract class must override
the abstract methods of the parent (define it),
or it too will be considered abstract
An abstract method cannot be defined as
final or static
The use of abstract classes is an important
element of software design – it allows us to
establish common elements in a hierarchy
that are too generic to instantiate
Final keyword
The final keyword in java is used to
restrict the user. The java final keyword
can be used in many context. Final can be:
variable
method
Class
Java final variable
If you make any variable as final, you cannot change the
value of final variable(It will be constant).

class Bike{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike obj=new Bike();
obj.run();
}
}//end of class
2) Java final method
 If you make any method as final, you cannot override it.
 Example of final method
class Bike{
final void run(){System.out.println("running");}
}

class Honda extends Bike{


void run()
{System.out.println("running safely with 100kmph");}

public static void main(String args[]){


Honda honda= new Honda();
honda.run();
}
}
 Test it Now Output:Compile Time Error
Java final class
If you make any class as final, you cannot
extend it.
Example of final class

final class Bike{}

class Honda extends Bike{


void run(){System.out.println("running safely with 100kmph");}

public static void main(String args[]){


Honda honda= new Honda();
honda.run();
}
}
Test it Now Output:Compile Time Error

You might also like