Inheritance

 UNIT-2

Inheritance in java is a mechanism in which one object acquires the properties and behaviors of another object.

Advantages of inheritance in java

1.For Method overriding (so runtime Polymorphism can be achieved).

2.For Code Reusability, Extend the functionality.

3.It allows creation of Hierarchical Classification.

Syntax:

class Subclass-name extends Superclass-name

{

//methods and fields of sub class

}

 

Types of inheritance

1.Single Level Inheritance:

A super class is inherited by only one sub class.


2.Multi Level Inheritance:

A sub-class will be inheriting parent class and as well as, the sub-class act as a parent class to other class., and so on.


  3.Hierarchical Inheritance:

A Super is inherited by many sub classes.


 

 

4.Multiple Inheritance: (not supported by java)

A sub class extending more than one super class.


  5. Hybrid Inheritance: (not supported by java)

It is a combination of Multiple and Multilevel inheritance.

 

….………………………………………………………………………….

Method Overriding:

when a method in a subclass has the same  name and type signature as a method in its super class, then the method in subclass is said to override the method in super class.

….…………………………………………………

Using Dynamic method dispatch(DMD), a call to an overridden method is resolved at runtime, rather than at compile time.

Runtime polymorphism in java implemented by using Dynamic method dispatch.

Upcasting: A super class reference variable can refer to a sub class object. I.e. When reference variable of super class refers to the sub class object, then it is called up casting.

….………………………………………………………………..

super keyword refers to the parent class.

1.It is used to access parent class methods or variables.

2.It can call the superclass constructor using super().

3.It helps to differentiate parent and child class members with the same name.

4.It must be the first statement in a constructor if used to call super().

5.It supports method overriding to include parent logic in child methods.

….………………………………………………………………………………………………

Inheritance/Runtime Polymorphism/ Dynamic Method Dispatch]/ super keyword example program

class Animal {

    void sound() {

        System.out.println("Animal sound");

    }

}

class Dog extends Animal {

    void sound() {

        super.sound(); // calls parent class method

        System.out.println("Dog barks");

    }

}

public class Test {

    public static void main(String[] args) {

        Animal a = new Dog(); // dynamic dispatch

        a.sound();            // runtime polymorphism

    }

}

….………………………………………………………

Final keyword

final keyword in Java is used to restrict modification. It can be applied to variables, methods, and classes.

·  final variable → cannot be changed

·  final method → cannot be overridden

·  final class → cannot be extended

final class Vehicle {

    final int speed = 60;

    final void showSpeed() {

        System.out.println("Speed: " + speed);

    }

}

class Test {

    public static void main(String[] args) {

        Vehicle v = new Vehicle();

        v.showSpeed();

    }

}

….…………………………….

Abstract classes

Abstraction is a process of hiding implementation details and showing only functionality to the users.

o Eg: Sending a Message, Driving a car etc.

Ø Abstraction in java achieved using

o Abstract classes (0 to 100%)

o Interfaces (100%)

Any class that contains one or more abstract methods, then the class is defines as abstract class.    An abstract method has no body defined in it.Subclass will provide the code for abstract method.

abstract class Shape {

    abstract void draw();

}

class Circle extends Shape {

    void draw() {

        System.out.println("Drawing Circle");

    }

}

class Test {

    public static void main(String[] args) {

        Shape s = new Circle();

        s.draw();

    }

}

….…………………………………………………………….

Interface

Definition:
An interface in Java is a blueprint of a class that contains abstract methods (by default) and constants. It is used to achieve 100% abstraction and multiple inheritance.

1. A class implements an interface using the implements keyword and must define all its methods.

2. Interfaces support multiple inheritance (a class can implement multiple interfaces).

interface Drawable {

    void draw();

}

 

class Circle implements Drawable {

    public void draw() {

        System.out.println("Drawing Circle");

    }

}

 

class Test {

    public static void main(String[] args) {

        Drawable d = new Circle();

        d.draw();

    }

}

….…………………………………………………..

//explain how Multiple inheritance achieving using interface in java


   ……………………………….

Access Modifiers & Access protection in Java

There are 4 types of Java access modifiers:

 Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.

Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.

Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.

Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

 


 


In Java, a package is a namespace that organizes classes and interfaces into a structured directory. It helps avoid name conflicts and makes code easier to manage.

 

 Common built-in packages:

1. java.util – utilities (e.g., ArrayList, Scanner)

2. java.io – input/output (e.g., File, BufferedReader)

3. java.lang – core classes (e.g., String, Math) – auto imported

4. java.net – networking (e.g., Socket, URL)

5. java.sql – database access (e.g., Connection, ResultSet)

User defined /customized packages example program

package Measure;

public class Converter

{

public float mmtocm(float mm)

{

float cm=(mm/10);

return cm;

}

public float cmtom(float cm)

{

float m=(cm/100);

return m;

}

public float mtokm(float m)

{

float km=(m/1000);

return km;

}

}

 

import Measure.Converter;

public class NeedConverter

{

public static void main(String args[])

{

Converter c=new Converter();

System.out.println(" mm to cm is  "+c.mmtocm(100));

System.out.println(" cm to m is  "+c.cmtom(1000));

System.out.println(" m to km is  "+c.mtokm(3000));

}

}

Comments

Popular posts from this blog

Java Programming Preparation

Java Programming Syllabus

Unit-I (Introduction to Java)