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

Unit 3 - Lecture-3

This document discusses object-oriented programming concepts in Java including inheritance, polymorphism, and accessing superclass methods and variables. It provides examples of overriding and hiding methods, using the super keyword to call superclass methods from subclasses, and how private methods cannot be overridden but can be hidden in subclasses.

Uploaded by

VORTEX GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit 3 - Lecture-3

This document discusses object-oriented programming concepts in Java including inheritance, polymorphism, and accessing superclass methods and variables. It provides examples of overriding and hiding methods, using the super keyword to call superclass methods from subclasses, and how private methods cannot be overridden but can be hidden in subclasses.

Uploaded by

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

Object Oriented Programming

Prof. Rahul B. Diwate

1
Unit-3
• Inheritance:
• Inheritance in Java,
• Types,
• Constructor in Inheritance,
• Using final with Inheritance,
• Accessing superclass member,
• Override private methods,
• Parent and Child classes having same data member,
• Base vs derived class reference.
• Polymorphism:
• Method Overloading,
• Overloading main(),
• Static vs Dynamic Binding,
• Method Hiding.
• Private and final methods,
• Passing and Returning Objects in Java
Accessing superclass member using “Super” Keyword
Previously we saw that the same method in the subclass overrides the method in
superclass.

In such a situation, the super keyword is used to call the method of the parent class
from the method of the child class.

example, the eat() method is present in both the base class Animal and the derived
class Dog. Notice the statement,
super.eat();
Here, the super keyword is used to call the eat() method present in the superclass.
class Animal { // new method in subclass
// method in the superclass public void bark() {
public void eat() { System.out.println("I can bark");
}}
System.out.println("I can eat");
class Main {
}} public static void main(String[] args) {
// Dog inherits Animal // create an object of the subclass
class Dog extends Animal { Dog labrador = new Dog();
// overriding the eat() method@Override // call the eat() method
public void eat() { labrador.eat();
labrador.bark();
// call method of superclass
}}
super.eat();
System.out.println("I eat dog food");
}
// Access Super Class Methods and Instance. Variables With Super Keyword in Java
import java.io.*;
// super class
class Welcome {
// instance variable
String name = "Welcome is the name";
void print()
{
System.out.println("This is the Welcome class");
} }
// derived class
class Demo1 extends Welcome {
// invoking the instance variable of parent class
String name = super.name;
void print()
{
// calling the overriden method
super.print();
System.out.println("This is the Demo1 class");
// printing the name
System.out.println(name);
} }
class Sample {
public static void main(String[] args)
{
// instance of the derived class
Demo1 ob = new Demo1();
// calling the unoverriden method print
ob.print();
Overriding Private Method,
Hiding Fields & Methods
• We have two classes: A child class Boy and a parent class Human. The Boy class
extends Human class. Both the classes have a common method void eat(). Boy class
is giving its own implementation to the eat() method or in other words it is
overriding the eat() method.
• The purpose of Method Overriding is clear here. Child class wants to give its own
implementation so that when it calls this method, it prints Boy is eating instead of
Human is eating.
Example
public class Private_Method{

public static void main(String args[]) {


//shows that private method can not be overridden in Java
Parent parent = new Child();
} }

class Parent{

public Parent(){
name();
normal();
}

private void name(){


System.out.printf("private method inside Parent class in Java %n");
}

public void normal(){


System.out.println("non private method from Parent class can be overridden");
} }

class Child extends Parent{

// Private methods can not be overridden in Java, they can only be hidden
private void name(){
System.out.printf("private method inside Child class in Java %n");
}

@Override
public void normal(){
System.out.println("non private overridden method from Child class ");
}

You might also like