Download presentation
Presentation is loading. Please wait.
Published byCynthia Knight Modified over 8 years ago
1
Inheritance
2
Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A relationship Also known as parent-child relationship. It provides code reusability
3
Syntax class Subclass-name extends Superclass-name { //methods and fields } The extends keyword indicates that we are making a new class that derives from an existing class.
4
Example of inheritance subclass : Programmer Superclass : Employee Relationship : Programmer IS-A Employee It means that Programmer is a type of Employee
5
Example of inheritance class Employee{ float salary=70000; } class Programmer extends Employee{ int bonus=40000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } Programmer salary is:70000.0 Bonus of programmer is:40000
6
Types of inheritance
7
Multiple inheritance When a class extends multiple classes i.e. known as multiple inheritance. For Example:
8
Why multiple inheritance is not supported in java? To reduce the complexity and simplify 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.
9
Why multiple inheritance is not supported in java? 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? } Compile Time Error Since compile time errors are better than runtime errors, java renders compile time error if you inherit more than one class.
10
Access Control Encapsulation provides access control By controlling access, we can prevent misuse. access modifiers are oprivate oprotected opublic odefault protected applies only when inheritance is involved
11
No Access Modifier When no access modifier is used, then it is default member of a class. The default modifier is accessible only within package, but cannot be accessed outside of its package There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.
12
Example of default access modifier //save by A.java package pack; class A{ void msg(){System.out.println("He llo");} } //save by B.java package mypack; import pack.*; class B{ public static void main(String args[]){ A obj = new A();//Compile Time Error obj.msg();//Compile Time Error }
13
public & private Public member can be accessed by any other code Private member can only be accessed by other members of its class why main( ) is public ? Ans: Main is called by code that is outside the program—that is, by the Java run-time system
14
private access modifier class A{ private int data=40; private void msg(){System.out.println("Hello java");} } public class Simple{ public static void main(String args[]){ A obj=new A(); System.out.println(obj.data);//Compile Time Error obj.msg();//Compile Time Error }
15
Example of public access modifier //save by A.java package pack; public class A{ public void msg(){System.out.println("Hello");} } //save by B.java package mypack; import pack.*; class B{ public static void main(String args[]){ A obj = new A(); obj.msg(); } Output:Hello
16
public & private class Test { int a; // default access public int b; // public access private int c; // private access // methods to access c void setc(int i) { // set c's value c = i; } int getc() { // get c's value return c; } class AccessTest { public static void main(String args[]) { Test ob = new Test(); // These are OK, a and b may be accessed directly ob.a = 10; ob.b = 20; // This is not OK and will cause an error // ob.c = 100; // Error! // You must access c through its methods ob.setc(100); // OK System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc()); }
17
protected access modifier The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. Protected modifier can't be applied on the class.
18
Example of protected access modifier //save by A.java package pack; public class A{ protected void msg(){System.out.println("Hello");} } //save by B.java package mypack; import pack.*; class B extends A{ public static void main(String args[]){ B obj = new B(); obj.msg(); } Output:Hello
19
All access modifiers Access Modifierwithin classwithin package outside package by subclass only outside package PrivateYNNN DefaultYYNN ProtectedYYYN PublicYYYY
20
Java Programs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.