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

Java Oops

Uploaded by

raxewo3630
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java Oops

Uploaded by

raxewo3630
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 9

Java Oops

Class and Constuctor


import java.util.*;

class Oops {

public static void main(String[] args) {

Student st=new Student();

st.name="Vijay";

st.roll_num=55;

st.marks=85.6;

st.greet();

class Student{

int roll_num;

String name;

double marks;

Student(){}

Student(int roll_num,String name,double marks){

this.roll_num=roll_num;

this.name=name;

this.marks=marks;

void greet(){

System.out.println("Roll Number is: "+this.roll_num);

System.out.println("Name is: "+this.name);


System.out.println("Marks is: "+this.marks);

Final Keyword
In Java, the final keyword is a modifier that can be applied to variables, methods, and classes,
and it serves different purposes depending on where it is used.

1.Final Variables: When a variable is declared as final, it means that its value cannot be changed once it
has been assigned.

always inilized when declaired => final int b;// cause error

final int a= 100;

a=1000 // this will cause error

2. Final Methods

A method declared as final cannot be overridden by subclasses. This is useful when you want to prevent
changes to a method's implementation.

class Parent {

final void display() {

System.out.println("This is a final method.");

class Child extends Parent {} //This will cause a compilation error

3. Final Classes

A class declared as final cannot be subclassed. This is useful for creating immutable classes or for
security reasons.

final class ImmutableClass {

// Class implementation

// class SubClass extends ImmutableClass { // This will cause a compilation error


// }

Static Method
import java.util.*;

class Oops {

public static void main(String[] args) {

greet();

void greet(){

System.out.println("Roll Number is: Vijay");

}// error due static method can only access static data/method;

static method can access only static method or data, but we can access static member method or data
inside the non-static member;

Super Keyword

import java.util.*;

class Animal{

int a=10;

void walk(){

System.out.println("Animal can walk and run");

Animal(){

System.out.println("animal is created");

}
class Dog extends Animal{

int a=15;

Dog(){

super();

System.out.println("dog is created");

void walk(){

System.out.println("Dog can walk and run");

super.walk();

void valueA(){

System.out.println(a);

System.out.println(super.a);

public class TestSuper3{

public static void main(String args[]){

Dog d=new Dog();

d.walk();

d.valueA();

Polymorphisms
1.Complile Time Polymorphisms achieved by Method Overloading

All the method are in same body;


import java.util.*;

class Bike{

int run(int a,int b){

return a+b;

int run(int a,int b,int c){

return a+b+c;

public class A{

public static void main(String args[]){

Bike b = new Bike();//upcasting

System.out.println(b.run(2,3));

System.out.println(b.run(2,3,4));

2.Run Time Polymorphisms achieved by Method overriding

When a method that is present in both parent and child has same name, return type, type of parameter
and number of parameters is same but they are in different class or body;

import java.util.*;

class Shape{

void draw(){

System.out.println("drawing...");

}
class Rectangle extends Shape{

void draw(){

System.out.println("drawing rectangle...");

super.draw();

public class A{

public static void main(String args[]){

Shape b = new Rectangle();

b.draw();

Interface

1.Multiple inheritance
public interface Engine {
void start ();
void stop ();
}

interface Fun {
void song ();
void stop ();
}

interface Break {
void acc ();
}

public class Car implements Engine,Break,Fun{


private int a=5;
public int getA(){
return a;
}
@Override
public void start(){
System.out.println("Song is starting");
}
@Override
public void stop(){
System.out.println("Car is stop");
}
@Override
public void acc(){
System.out.println("Car is accelerating");
}
@Override
public void song(){
System.out.println("Car is with multimedia functionality");
}
}

public class Main {


public static void main(String[] args) {
Engine car =new Car();
car.start();
car.stop();
// car.acc();
// car.song();
// System.out.println(car.getA());
}
}

2.Interface inherit interface


public interface Engine {
void start();
void stop();
}

interface funct extends Engine{


void song();
void stop();
}
public class Car implements funct{
@Override
public void start(){
System.out.println("Song is starting");
}
@Override
public void stop(){
System.out.println("Car is stop");
}
@Override
public void song(){
System.out.println("Car is with multimedia functionality");
}
}

public class Main {


public static void main(String[] args) {
funct car =new Car();
car.start();
car.stop();
car.song();
}
}

Generic
import java.util.*;

class Car<E,V>{

E id;

V name;

public Car(E id,V name){

this.id=id;

this.name=name;

void solve(){

System.out.println(this.id +" "+ this.name);


}

public class Main {

public static void main(String[] args) {

Car<Integer,String>car=new Car<>(2,"Vijay");

car.solve();

You might also like